MCPcopy Create free account
hub / github.com/cortexproject/cortex / TestStateReplication

Function TestStateReplication

pkg/alertmanager/state_replication_test.go:98–202  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

96}
97
98func TestStateReplication(t *testing.T) {
99 tc := []struct {
100 name string
101 replicationFactor int
102 message *clusterpb.Part
103 results map[string]*clusterpb.Part
104 }{
105 {
106 name: "with a replication factor of <= 1, state is not replicated.",
107 replicationFactor: 1,
108 message: &clusterpb.Part{Key: "nflog", Data: []byte("OK")},
109 results: map[string]*clusterpb.Part{},
110 },
111 {
112 name: "with a replication factor of > 1, state is broadcasted for replication.",
113 replicationFactor: 3,
114 message: &clusterpb.Part{Key: "nflog", Data: []byte("OK")},
115 results: map[string]*clusterpb.Part{"user-1": {Key: "nflog", Data: []byte("OK")}},
116 },
117 }
118
119 for _, tt := range tc {
120 t.Run(tt.name, func(t *testing.T) {
121 reg := prometheus.NewPedanticRegistry()
122 replicator := newFakeReplicator()
123 replicator.read = readStateResult{res: nil, err: nil}
124 store := newFakeAlertStore()
125 s := newReplicatedStates("user-1", tt.replicationFactor, replicator, store, log.NewNopLogger(), reg)
126
127 require.False(t, s.Ready())
128 {
129 ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
130 defer cancel()
131 require.Equal(t, context.DeadlineExceeded, s.WaitReady(ctx))
132 }
133
134 require.NoError(t, services.StartAndAwaitRunning(context.Background(), s))
135 t.Cleanup(func() {
136 require.NoError(t, services.StopAndAwaitTerminated(context.Background(), s))
137 })
138
139 require.True(t, s.Ready())
140 {
141 ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
142 defer cancel()
143 require.NoError(t, s.WaitReady(ctx))
144 }
145
146 ch := s.AddState("nflog:user-1", &fakeState{}, reg)
147
148 part := tt.message
149 d, err := part.Marshal()
150 require.NoError(t, err)
151 ch.Broadcast(d)
152
153 require.Eventually(t, func() bool {
154 replicator.mtx.Lock()
155 defer replicator.mtx.Unlock()

Callers

nothing calls this directly

Calls 12

StartAndAwaitRunningFunction · 0.92
StopAndAwaitTerminatedFunction · 0.92
newFakeReplicatorFunction · 0.85
newFakeAlertStoreFunction · 0.85
newReplicatedStatesFunction · 0.85
RunMethod · 0.65
ReadyMethod · 0.65
EqualMethod · 0.65
WaitReadyMethod · 0.65
AddStateMethod · 0.65
MarshalMethod · 0.45
BroadcastMethod · 0.45

Tested by

no test coverage detected