You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.2 KiB

  1. package util
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. )
  7. func TestAsyncPool(t *testing.T) {
  8. p := NewLimitedAsyncExecutor(3)
  9. var results []Future
  10. results = append(results, p.Execute(FirstFunc))
  11. results = append(results, p.Execute(SecondFunc))
  12. results = append(results, p.Execute(ThirdFunc))
  13. results = append(results, p.Execute(FourthFunc))
  14. results = append(results, p.Execute(FifthFunc))
  15. for _, r := range results {
  16. x := r.Await().(int)
  17. println(x)
  18. }
  19. }
  20. func FirstFunc() any {
  21. fmt.Println("-- Executing first function --")
  22. time.Sleep(7 * time.Second)
  23. fmt.Println("-- First Function finished --")
  24. return 1
  25. }
  26. func SecondFunc() any {
  27. fmt.Println("-- Executing second function --")
  28. time.Sleep(5 * time.Second)
  29. fmt.Println("-- Second Function finished --")
  30. return 2
  31. }
  32. func ThirdFunc() any {
  33. fmt.Println("-- Executing third function --")
  34. time.Sleep(2 * time.Second)
  35. fmt.Println("-- Third Function finished --")
  36. return 3
  37. }
  38. func FourthFunc() any {
  39. fmt.Println("-- Executing fourth function --")
  40. time.Sleep(10 * time.Second)
  41. fmt.Println("-- Fourth Function finished --")
  42. return 4
  43. }
  44. func FifthFunc() any {
  45. fmt.Println("-- Executing fifth function --")
  46. time.Sleep(4 * time.Second)
  47. fmt.Println("-- Fourth fifth finished --")
  48. return 5
  49. }