Vova Tkach 5 years ago
parent
commit
5daef1d1a6
1 changed files with 46 additions and 0 deletions
  1. 46 0
      worker/worker_test.go

+ 46 - 0
worker/worker_test.go

@@ -1 +1,47 @@
 package worker
+
+import (
+	"time"
+	"errors"
+	"context"
+	"testing"
+)
+
+type SomeTest struct {
+	Variable bool
+	Done     chan bool
+}
+
+func compareResults(v *SomeTest) (bool, error) {
+	worker := New(func(ctx context.Context, w *Worker, o *[]Iface) {
+		if sb, ok := (*o)[0].(*SomeTest); ok {
+			sb.Variable = true
+			sb.Done <- true
+		}
+		w.Shutdown(nil)
+	}, &[]Iface{
+		v,
+	})
+
+	select {
+	case <-time.After(5 * time.Second):
+		return false, errors.New("TIMEOUT")
+	case <-v.Done:
+		return v.Variable, worker.Shutdown(nil)
+	}
+
+	return v.Variable, worker.Shutdown(nil)
+}
+
+func TestGoroutineAndChangeVariable(t *testing.T) {
+	someVar := SomeTest{Variable: false, Done: make(chan bool)}
+	boolVar, err := compareResults(&someVar)
+
+	if boolVar != true {
+		t.Fatalf("should modify variable\n")
+	}
+
+	if err != nil {
+		t.Fatalf("should be nil\n")
+	}
+}