|
1 | 1 | package gossip |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "testing" |
5 | 6 | "time" |
6 | 7 |
|
| 8 | + "github.com/hashicorp/memberlist" |
| 9 | + "github.com/stretchr/testify/assert" |
7 | 10 | pubsub "go.linka.cloud/pubsub/typed" |
8 | 11 | "google.golang.org/grpc" |
9 | 12 |
|
10 | | - "github.com/stretchr/testify/assert" |
11 | | - |
12 | 13 | "go.linka.cloud/protodb/internal/badgerd/replication" |
| 14 | + pb2 "go.linka.cloud/protodb/internal/badgerd/replication/gossip/pb" |
13 | 15 | ) |
14 | 16 |
|
15 | 17 | func TestGossipSimpleMethodsTable(t *testing.T) { |
@@ -49,3 +51,37 @@ func TestGossipSimpleMethodsTable(t *testing.T) { |
49 | 51 | assert.NotNil(t, conn) |
50 | 52 | assert.Same(t, cc, conn) |
51 | 53 | } |
| 54 | + |
| 55 | +func TestRunSkipsElectionWhenLeaderAlreadyKnown(t *testing.T) { |
| 56 | + ctx, cancel := context.WithCancel(context.Background()) |
| 57 | + defer cancel() |
| 58 | + |
| 59 | + r := &Gossip{ |
| 60 | + ctx: ctx, |
| 61 | + name: "self", |
| 62 | + shuttingDown: NewAtomic(false), |
| 63 | + leading: NewAtomic(false), |
| 64 | + leaderName: NewAtomic("peer"), |
| 65 | + bootNodes: []string{"peer"}, |
| 66 | + ready: make(chan struct{}), |
| 67 | + events: make(chan memberlist.NodeEvent), |
| 68 | + converged: make(chan struct{}), |
| 69 | + pub: pubsub.NewPublisher[string](time.Second, 2), |
| 70 | + } |
| 71 | + r.meta.Store(&pb2.Meta{LocalVersion: 1}) |
| 72 | + close(r.converged) |
| 73 | + |
| 74 | + r.run(ctx) |
| 75 | + t.Cleanup(func() { close(r.events) }) |
| 76 | + |
| 77 | + time.Sleep(1500 * time.Millisecond) |
| 78 | + |
| 79 | + assert.Equal(t, "peer", r.CurrentLeader()) |
| 80 | + assert.False(t, r.IsLeader()) |
| 81 | + assert.False(t, r.HasLeader() && r.CurrentLeader() == "self") |
| 82 | + select { |
| 83 | + case <-r.ready: |
| 84 | + t.Fatal("expected ready to stay open") |
| 85 | + default: |
| 86 | + } |
| 87 | +} |
0 commit comments