mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 07:26:55 +08:00
refactor
This commit is contained in:
@@ -127,13 +127,12 @@ func TestSpawnWorkers(t *testing.T) {
|
||||
defer runner.close()
|
||||
|
||||
runner.client = newClient("localhost", 5557, runner.nodeID)
|
||||
runner.reset()
|
||||
runner.setTasks(tasks)
|
||||
runner.stopChan = make(chan bool)
|
||||
runner.rebalance = make(chan bool)
|
||||
go runner.spawnWorkers(10, 10, runner.stopChan, runner.spawnComplete)
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
currentClients := atomic.LoadInt32(&runner.currentClientsNum)
|
||||
currentClients := runner.controller.getCurrentClientsNum()
|
||||
if currentClients != 10 {
|
||||
t.Error("Unexpected count", currentClients)
|
||||
}
|
||||
@@ -163,17 +162,16 @@ func TestSpawnWorkersWithManyTasks(t *testing.T) {
|
||||
runner := newWorkerRunner("localhost", 5557)
|
||||
defer runner.close()
|
||||
|
||||
runner.reset()
|
||||
runner.setTasks(tasks)
|
||||
runner.client = newClient("localhost", 5557, runner.nodeID)
|
||||
|
||||
const numToSpawn int64 = 30
|
||||
runner.stopChan = make(chan bool)
|
||||
runner.rebalance = make(chan bool)
|
||||
|
||||
go runner.spawnWorkers(numToSpawn, float64(numToSpawn), runner.stopChan, runner.spawnComplete)
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
currentClients := atomic.LoadInt32(&runner.currentClientsNum)
|
||||
currentClients := runner.controller.getCurrentClientsNum()
|
||||
|
||||
assert.Equal(t, numToSpawn, int64(currentClients))
|
||||
lock.Lock()
|
||||
@@ -226,15 +224,15 @@ func TestSpawnAndStop(t *testing.T) {
|
||||
runner.client = newClient("localhost", 5557, runner.nodeID)
|
||||
|
||||
runner.setTasks(tasks)
|
||||
runner.spawn.setSpawn(10, 10)
|
||||
runner.updateState(StateSpawning)
|
||||
runner.setSpawnCount(10)
|
||||
runner.setSpawnRate(10)
|
||||
|
||||
go runner.start()
|
||||
|
||||
// wait for spawning goroutines
|
||||
time.Sleep(2 * time.Second)
|
||||
if atomic.LoadInt32(&runner.currentClientsNum) != 10 {
|
||||
t.Error("Number of goroutines mismatches, expected: 10, current count", atomic.LoadInt32(&runner.currentClientsNum))
|
||||
if runner.controller.getCurrentClientsNum() != 10 {
|
||||
t.Error("Number of goroutines mismatches, expected: 10, current count", runner.controller.getCurrentClientsNum())
|
||||
}
|
||||
|
||||
msg := <-runner.client.sendChannel()
|
||||
@@ -258,10 +256,8 @@ func TestStop(t *testing.T) {
|
||||
}
|
||||
tasks := []*Task{taskA}
|
||||
runner := newWorkerRunner("localhost", 5557)
|
||||
runner.stopChan = make(chan bool)
|
||||
runner.rebalance = make(chan bool)
|
||||
runner.setTasks(tasks)
|
||||
runner.spawn.setSpawn(10, 10)
|
||||
runner.reset()
|
||||
runner.updateState(StateSpawning)
|
||||
|
||||
runner.stop()
|
||||
@@ -281,20 +277,21 @@ func TestOnSpawnMessage(t *testing.T) {
|
||||
defer runner.close()
|
||||
runner.client = newClient("localhost", 5557, runner.nodeID)
|
||||
runner.updateState(StateInit)
|
||||
runner.reset()
|
||||
runner.setTasks([]*Task{taskA})
|
||||
runner.spawn.spawnCount = 100
|
||||
runner.spawn.spawnRate = 100
|
||||
runner.setSpawnCount(100)
|
||||
runner.setSpawnRate(100)
|
||||
|
||||
runner.onSpawnMessage(newGenericMessage("spawn", map[string]int64{
|
||||
"spawn_count": 20,
|
||||
"spawn_rate": 20,
|
||||
}, runner.nodeID))
|
||||
|
||||
if runner.spawn.spawnCount != 20 {
|
||||
t.Error("workers should be overwrote by onSpawnMessage, expected: 20, was:", runner.spawn.spawnCount)
|
||||
if runner.getSpawnCount() != 20 {
|
||||
t.Error("workers should be overwrote by onSpawnMessage, expected: 20, was:", runner.controller.spawnCount)
|
||||
}
|
||||
if runner.spawn.spawnRate != 20 {
|
||||
t.Error("spawnRate should be overwrote by onSpawnMessage, expected: 20, was:", runner.spawn.spawnRate)
|
||||
if runner.getSpawnRate() != 20 {
|
||||
t.Error("spawnRate should be overwrote by onSpawnMessage, expected: 20, was:", runner.controller.spawnRate)
|
||||
}
|
||||
|
||||
runner.onMessage(newGenericMessage("stop", nil, runner.nodeID))
|
||||
@@ -309,9 +306,8 @@ func TestOnQuitMessage(t *testing.T) {
|
||||
<-runner.closeChan
|
||||
|
||||
runner.updateState(StateRunning)
|
||||
runner.reset()
|
||||
runner.closeChan = make(chan bool)
|
||||
runner.stopChan = make(chan bool)
|
||||
runner.rebalance = make(chan bool)
|
||||
runner.client.shutdownChan = make(chan bool)
|
||||
runner.onMessage(newGenericMessage("quit", nil, runner.nodeID))
|
||||
<-runner.closeChan
|
||||
@@ -321,7 +317,7 @@ func TestOnQuitMessage(t *testing.T) {
|
||||
|
||||
runner.updateState(StateStopped)
|
||||
runner.closeChan = make(chan bool)
|
||||
runner.stopChan = make(chan bool)
|
||||
runner.reset()
|
||||
runner.client.shutdownChan = make(chan bool)
|
||||
runner.onMessage(newGenericMessage("quit", nil, runner.nodeID))
|
||||
<-runner.closeChan
|
||||
@@ -344,7 +340,6 @@ func TestOnMessage(t *testing.T) {
|
||||
tasks := []*Task{taskA, taskB}
|
||||
|
||||
runner := newWorkerRunner("localhost", 5557)
|
||||
defer runner.close()
|
||||
runner.client = newClient("localhost", 5557, runner.nodeID)
|
||||
runner.updateState(StateInit)
|
||||
runner.setTasks(tasks)
|
||||
@@ -364,8 +359,8 @@ func TestOnMessage(t *testing.T) {
|
||||
|
||||
// spawn complete and running
|
||||
time.Sleep(2 * time.Second)
|
||||
if atomic.LoadInt32(&runner.currentClientsNum) != 10 {
|
||||
t.Error("Number of goroutines mismatches, expected: 10, current count:", atomic.LoadInt32(&runner.currentClientsNum))
|
||||
if runner.controller.getCurrentClientsNum() != 10 {
|
||||
t.Error("Number of goroutines mismatches, expected: 10, current count:", runner.controller.getCurrentClientsNum())
|
||||
}
|
||||
msg = <-runner.client.sendChannel()
|
||||
if msg.Type != "spawning_complete" {
|
||||
@@ -376,22 +371,17 @@ func TestOnMessage(t *testing.T) {
|
||||
}
|
||||
|
||||
// increase goroutines while running
|
||||
runner.onMessage(newGenericMessage("spawn", map[string]int64{
|
||||
runner.onMessage(newGenericMessage("rebalance", map[string]int64{
|
||||
"spawn_count": 15,
|
||||
"spawn_rate": 15,
|
||||
}, runner.nodeID))
|
||||
|
||||
msg = <-runner.client.sendChannel()
|
||||
if msg.Type != "spawning" {
|
||||
t.Error("Runner should send spawning message when starting spawn, got", msg.Type)
|
||||
}
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
if runner.getState() != StateRunning {
|
||||
t.Error("State of runner is not running after spawn, got", runner.getState())
|
||||
}
|
||||
if atomic.LoadInt32(&runner.currentClientsNum) != 15 {
|
||||
t.Error("Number of goroutines mismatches, expected: 20, current count:", atomic.LoadInt32(&runner.currentClientsNum))
|
||||
if runner.controller.getCurrentClientsNum() != 15 {
|
||||
t.Error("Number of goroutines mismatches, expected: 15, current count:", runner.controller.getCurrentClientsNum())
|
||||
}
|
||||
|
||||
// stop all the workers
|
||||
@@ -404,7 +394,7 @@ func TestOnMessage(t *testing.T) {
|
||||
t.Error("Runner should send client_stopped message, got", msg.Type)
|
||||
}
|
||||
|
||||
time.Sleep(3 * time.Second)
|
||||
time.Sleep(4 * time.Second)
|
||||
|
||||
go runner.start()
|
||||
// spawn again
|
||||
@@ -420,8 +410,8 @@ func TestOnMessage(t *testing.T) {
|
||||
|
||||
// spawn complete and running
|
||||
time.Sleep(3 * time.Second)
|
||||
if atomic.LoadInt32(&runner.currentClientsNum) != 10 {
|
||||
t.Error("Number of goroutines mismatches, expected: 10, current count:", atomic.LoadInt32(&runner.currentClientsNum))
|
||||
if runner.controller.getCurrentClientsNum() != 10 {
|
||||
t.Error("Number of goroutines mismatches, expected: 10, current count:", runner.controller.getCurrentClientsNum())
|
||||
}
|
||||
if runner.getState() != StateRunning {
|
||||
t.Error("State of runner is not running after spawn, got", runner.getState())
|
||||
@@ -440,13 +430,17 @@ func TestOnMessage(t *testing.T) {
|
||||
if msg.Type != "client_stopped" {
|
||||
t.Error("Runner should send client_stopped message, got", msg.Type)
|
||||
}
|
||||
|
||||
// quit
|
||||
runner.onMessage(newGenericMessage("quit", nil, runner.nodeID))
|
||||
}
|
||||
|
||||
func TestClientListener(t *testing.T) {
|
||||
runner := newMasterRunner("localhost", 5557)
|
||||
defer runner.close()
|
||||
runner.updateState(StateInit)
|
||||
runner.spawn.setSpawn(10, 10)
|
||||
runner.setSpawnCount(10)
|
||||
runner.setSpawnRate(10)
|
||||
go runner.clientListener()
|
||||
runner.server.clients.Store("testID1", &WorkerNode{ID: "testID1", Heartbeat: 3})
|
||||
runner.server.clients.Store("testID2", &WorkerNode{ID: "testID2", Heartbeat: 3})
|
||||
@@ -496,7 +490,8 @@ func TestHeartbeatWorker(t *testing.T) {
|
||||
runner := newMasterRunner("localhost", 5557)
|
||||
defer runner.close()
|
||||
runner.updateState(StateInit)
|
||||
runner.spawn.setSpawn(10, 10)
|
||||
runner.setSpawnCount(10)
|
||||
runner.setSpawnRate(10)
|
||||
runner.server.clients.Store("testID1", &WorkerNode{ID: "testID1", Heartbeat: 1, State: StateInit})
|
||||
runner.server.clients.Store("testID2", &WorkerNode{ID: "testID2", Heartbeat: 1, State: StateInit})
|
||||
go runner.clientListener()
|
||||
|
||||
Reference in New Issue
Block a user