Repository navigation
Expand file tree
/
Copy pathpump_test.go
More file actions
88 lines (83 loc) · 2.13 KB
/
Copy pathpump_test.go
File metadata and controls
88 lines (83 loc) · 2.13 KB
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
package rchan
import (
"context"
"testing"
"time"
)
func TestNativeSelect(t *testing.T) {
ctx := context.Background()
rdb := testClient(t)
a, _ := New[string](ctx, rdb, testName(t, rdb), WithCapacity(2))
b, _ := New[string](ctx, rdb, testName(t, rdb), WithCapacity(2))
defer a.Stop()
defer b.Stop()
if err := b.Send(ctx, "from-b"); err != nil {
t.Fatal(err)
}
select {
case v := <-a.C():
t.Fatalf("unexpected value on a: %q", v)
case v := <-b.C():
if v != "from-b" {
t.Fatalf("v=%q", v)
}
case <-time.After(5 * time.Second):
t.Fatal("select timed out")
}
}
func TestProxyClosesOnDrain(t *testing.T) {
ctx := context.Background()
rdb := testClient(t)
ch, _ := New[int](ctx, rdb, testName(t, rdb), WithCapacity(4))
defer ch.Stop()
ch.Send(ctx, 1)
ch.Send(ctx, 2)
if err := ch.Close(ctx); err != nil {
t.Fatal(err)
}
var got []int
for v := range ch.C() { // ends only if the proxy chan closes
got = append(got, v)
}
if len(got) != 2 || got[0] != 1 || got[1] != 2 {
t.Fatalf("got %v", got)
}
}
func TestPumpAcksOnConsume(t *testing.T) {
ctx := context.Background()
rdb := testClient(t)
name := testName(t, rdb)
ch, _ := New[string](ctx, rdb, name, WithCapacity(2))
defer ch.Stop()
ch.Send(ctx, "a")
proxy := ch.C()
// Pump claims the value but must not ack until the app reads it.
eventually(t, 2*time.Second, "pump claims entry", func() bool {
p, err := rdb.XPending(ctx, "rchan:{"+name+"}:s", "recv").Result()
return err == nil && p.Count == 1
})
if n, _ := rdb.XLen(ctx, "rchan:{"+name+"}:s").Result(); n != 1 {
t.Fatalf("XLEN=%d, want 1 while unconsumed", n)
}
if v := <-proxy; v != "a" {
t.Fatalf("v=%q", v)
}
eventually(t, 2*time.Second, "acked after consume", func() bool {
n, _ := rdb.XLen(ctx, "rchan:{"+name+"}:s").Result()
return n == 0
})
}
func TestStopClosesProxy(t *testing.T) {
rdb := testClient(t)
ch, _ := New[int](context.Background(), rdb, testName(t, rdb), WithCapacity(2))
proxy := ch.C()
ch.Stop()
select {
case _, ok := <-proxy:
if ok {
t.Fatal("expected closed proxy after Stop")
}
case <-time.After(5 * time.Second):
t.Fatal("proxy not closed after Stop")
}
}