| 1 |
package types |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"testing" |
| 6 |
"time" |
| 7 |
) |
| 8 |
|
| 9 |
func TestWorkerCallback(t *testing.T) { |
| 10 |
worker := NewWorker("test", make(chan WorkerMessage)) |
| 11 |
|
| 12 |
ctx, cancel := context.WithCancel(context.Background()) |
| 13 |
defer cancel() |
| 14 |
go func() { |
| 15 |
for { |
| 16 |
select { |
| 17 |
case action := <-worker.Actions(): |
| 18 |
response := Message{ |
| 19 |
inResponseTo: action, |
| 20 |
Id: 2, |
| 21 |
} |
| 22 |
worker.ProcessMessage(&response) |
| 23 |
case <-ctx.Done(): |
| 24 |
return |
| 25 |
|
| 26 |
} |
| 27 |
} |
| 28 |
}() |
| 29 |
|
| 30 |
msg := Message{Id: 1} |
| 31 |
|
| 32 |
called := make(chan struct{}) |
| 33 |
worker.PostAction(context.TODO(), &msg, func(msg WorkerMessage) { |
| 34 |
close(called) |
| 35 |
}) |
| 36 |
|
| 37 |
select { |
| 38 |
case <-called: |
| 39 |
case <-time.After(time.Second): |
| 40 |
t.Errorf("callback was not called") |
| 41 |
} |
| 42 |
} |