-
Notifications
You must be signed in to change notification settings - Fork 0
/
bus_test.go
143 lines (110 loc) · 2.6 KB
/
bus_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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package bus_test
import (
"context"
"errors"
"fmt"
"net/http/httptest"
"os"
"sync"
"testing"
"time"
"ella.to/bus"
"ella.to/immuta"
"ella.to/task"
)
func createBusServer(t *testing.T, eventLogs string) *bus.Client {
os.Remove(eventLogs)
storage, err := immuta.New(eventLogs, 10, true)
if err != nil {
t.Fatal(err)
}
handler := bus.NewHandler(storage, task.NewRunner(task.WithWorkerSize(1)))
server := httptest.NewServer(handler)
t.Cleanup(func() {
storage.Close()
server.Close()
os.Remove(eventLogs)
})
return bus.NewClient(server.URL)
}
func TestBasicPutUsage(t *testing.T) {
client := createBusServer(t, "TestBasicUsage.log")
resp := client.Put(context.Background(), bus.WithSubject("a.b.c"), bus.WithData("hello world"))
if resp.Error() != nil {
t.Fatal(resp.Error())
}
fmt.Println(resp)
}
func TestBasicPutGetUsage(t *testing.T) {
client := createBusServer(t, "TestBasicPutGetUsage.log")
resp := client.Put(context.Background(), bus.WithSubject("a.b.c"), bus.WithData("hello world"))
if resp.Error() != nil {
t.Fatal(resp.Error())
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
for event, err := range client.Get(
ctx,
bus.WithSubject("a.b.c"),
bus.WithAckStrategy(bus.AckNone),
bus.WithStartFrom(bus.StartOldest),
) {
if !errors.Is(err, context.DeadlineExceeded) {
break
} else if err != nil {
t.Fatalf("failed to get event: %s", err)
}
if event.Id != resp.Id {
t.Fatalf("expected %s but get event id: %s", resp.Id, event.Id)
}
}
fmt.Println("done")
}
func TestSinglePutMultipleGet(t *testing.T) {
client := createBusServer(t, "TestBasicUsage.log")
n := 1_00
p := 10
c := 8
total := n * p
var wg sync.WaitGroup
wg.Add(c + p)
for range p {
go func() {
defer wg.Done()
for range n {
resp := client.Put(context.Background(), bus.WithSubject("a.b.c"), bus.WithData("hello world"))
if resp.Error() != nil {
t.Errorf("failed to publish: %s", resp.Error())
}
}
fmt.Println("DONE PUBLISHING")
}()
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
for i := range c {
go func(i int) {
defer wg.Done()
count := 0
for event, err := range client.Get(
ctx,
bus.WithSubject("a.b.c"),
bus.WithAckStrategy(bus.AckNone),
bus.WithStartFrom(bus.StartOldest),
) {
if err != nil {
t.Errorf("failed to get event: %s", err)
return
}
count++
if count == total {
break
}
_ = event
}
fmt.Printf("DONE CONSUMING (%d) : %d\n", i, count)
}(i)
}
wg.Wait()
fmt.Println("done")
}