pool_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package worker
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestIncrease(t *testing.T) {
  7. var (
  8. conf = &Conf{
  9. QueueSize: 10,
  10. WorkerProcMax: 10,
  11. WorkerNumber: 1,
  12. }
  13. workerPool = New(conf)
  14. )
  15. for i := 0; i < 10; i++ {
  16. workerPool.Add(longtime)
  17. }
  18. time.Sleep(6 * time.Second)
  19. var expect = minInt(conf.WorkerNumber<<1, conf.WorkerProcMax)
  20. if workerPool.workerNumber != expect {
  21. t.Logf("worker number=%d, expect=%d", workerPool.workerNumber, expect)
  22. t.FailNow()
  23. }
  24. for i := 0; i < 10; i++ {
  25. workerPool.Add(longtime)
  26. }
  27. time.Sleep(6 * time.Second)
  28. expect = minInt(conf.WorkerNumber<<2, conf.WorkerProcMax)
  29. if workerPool.workerNumber != expect {
  30. t.Logf("worker number=%d, expect=%d", workerPool.workerNumber, expect)
  31. t.FailNow()
  32. }
  33. for i := 0; i < 10; i++ {
  34. workerPool.Add(longtime)
  35. }
  36. time.Sleep(6 * time.Second)
  37. expect = minInt(conf.WorkerNumber<<3, conf.WorkerProcMax)
  38. if workerPool.workerNumber != expect {
  39. t.Logf("worker number=%d, expect=%d", workerPool.workerNumber, expect)
  40. t.FailNow()
  41. }
  42. for i := 0; i < 10; i++ {
  43. workerPool.Add(longtime)
  44. }
  45. time.Sleep(6 * time.Second)
  46. expect = minInt(conf.WorkerNumber<<4, conf.WorkerProcMax)
  47. if workerPool.workerNumber != expect {
  48. t.Logf("worker number=%d, expect=%d", workerPool.workerNumber, expect)
  49. t.FailNow()
  50. }
  51. }
  52. func longtime() {
  53. time.Sleep(20 * time.Second)
  54. }