worker_test.go 794 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package worker
  2. import (
  3. "context"
  4. "errors"
  5. "testing"
  6. "time"
  7. )
  8. type SomeTest struct {
  9. Variable bool
  10. Done chan bool
  11. }
  12. func compareResults(v *SomeTest) (bool, error) {
  13. worker := New(func(ctx context.Context, w *Worker, o *[]Iface) {
  14. if sb, ok := (*o)[0].(*SomeTest); ok {
  15. sb.Variable = true
  16. sb.Done <- true
  17. }
  18. w.Shutdown(nil)
  19. }, &[]Iface{
  20. v,
  21. })
  22. select {
  23. case <-time.After(5 * time.Second):
  24. return false, errors.New("TIMEOUT")
  25. case <-v.Done:
  26. return v.Variable, worker.Shutdown(nil)
  27. }
  28. }
  29. func TestGoroutineAndChangeVariable(t *testing.T) {
  30. someVar := SomeTest{Variable: false, Done: make(chan bool)}
  31. boolVar, err := compareResults(&someVar)
  32. if boolVar != true {
  33. t.Fatalf("should modify variable\n")
  34. }
  35. if err != nil {
  36. t.Fatalf("should be nil\n")
  37. }
  38. }