-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcoordinator_test.go
80 lines (69 loc) · 1.93 KB
/
coordinator_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
package tableroll
import (
"context"
"io"
"testing"
"github.com/inconshreveable/log15"
"github.com/stretchr/testify/require"
"k8s.io/utils/clock"
)
// TestConnectOwner is a happy-path test of using the coordinator
func TestConnectOwner(t *testing.T) {
l := log15.New()
ctx := context.Background()
tmpdir := tmpDir(t)
coord1 := newCoordinator(clock.RealClock{}, l, tmpdir, "1")
coord2 := newCoordinator(clock.RealClock{}, l, tmpdir, "2")
coord1l, err := coord1.Listen(ctx)
require.NoError(t, err)
require.NoError(t, coord1.Lock(ctx))
require.NoError(t, coord1.BecomeOwner())
require.NoError(t, coord1.Unlock())
connw, err := coord2.ConnectOwner(ctx)
require.NoError(t, err)
connErr := make(chan error, 1)
go func() {
defer close(connErr)
if _, err := connw.Write([]byte("hello world")); err != nil {
connErr <- err
}
if err := connw.Close(); err != nil {
connErr <- err
}
}()
connr, err := coord1l.Accept()
if err != nil {
t.Fatalf("acccept err: %v", err)
}
data, err := io.ReadAll(connr)
require.NoError(t, err)
require.Equal(t, "hello world", string(data))
require.NoError(t, <-connErr)
}
// TestLockCoordinationDirCtxCancel tests that a call to `lockCoordinationDir` can be
// canceled by canceling the passed in context.
func TestLockCoordinationDirCtxCancel(t *testing.T) {
l := log15.New()
ctx := testCtx(t)
tmpdir := tmpDir(t)
coord1 := newCoordinator(clock.RealClock{}, l, tmpdir, "1")
coord2 := newCoordinator(clock.RealClock{}, l, tmpdir, "2")
require.NoError(t, coord1.Lock(ctx))
defer func() { require.NoError(t, coord1.Unlock()) }()
ctx2, cancel := context.WithCancel(ctx)
coordErr := make(chan error)
go func() {
err := coord2.Lock(ctx2)
coordErr <- err
}()
select {
case err := <-coordErr:
t.Fatalf("expected no coord error, should be blocked: %v", err)
default:
}
cancel()
err := <-coordErr
if err != context.Canceled {
t.Errorf("expected context cancel, got %v", err)
}
}