generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessaging_test.go
93 lines (74 loc) · 1.8 KB
/
messaging_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package minibus_test
import (
"context"
"errors"
"fmt"
"sync/atomic"
"testing"
"time"
. "github.com/dogmatiq/minibus"
)
func TestRun_messaging(t *testing.T) {
t.Run("it does not exchange any messages until all functions are ready", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
var started atomic.Int32
err := Run(
ctx,
func(ctx context.Context) error {
// Delay a little to help induce the race condition we're
// testing for.
time.Sleep(10 * time.Millisecond)
Subscribe[string](ctx)
started.Add(1)
Ready(ctx)
m, err := Receive(ctx)
if err != nil {
return err
}
if started.Load() != 2 {
return fmt.Errorf("received a message before all signaled readiness: %q", m)
}
return nil
},
func(ctx context.Context) error {
started.Add(1)
Ready(ctx)
if err := Send(ctx, "<message>"); err != nil {
return err
}
if started.Load() != 2 {
return fmt.Errorf("sent a message before all functions signaled readiness")
}
return nil
},
)
if err != nil {
t.Fatalf("Run() returned an unexpected error: %s", err)
}
})
t.Run("it does not deliver messages to the function that sent them", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := Run(
ctx,
func(ctx context.Context) error {
Subscribe[string](ctx)
Ready(ctx)
err := Send(ctx, "<message>")
if err != nil {
return err
}
select {
case <-time.After(50 * time.Millisecond):
return nil
case <-Inbox(ctx):
return errors.New("function received a message from itself")
}
},
)
if err != nil {
t.Fatalf("Run() returned an unexpected error: %s", err)
}
})
}