|
1 | 1 | package orchestrator |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "math/big" |
5 | 6 | "testing" |
6 | 7 | "time" |
@@ -207,12 +208,61 @@ func TestStartCommitter(t *testing.T) { |
207 | 208 | mockStagingStorage.On("DeleteStagingData", &blockData).Return(nil) |
208 | 209 |
|
209 | 210 | // Start the committer in a goroutine |
210 | | - go committer.Start() |
| 211 | + go committer.Start(context.Background()) |
211 | 212 |
|
212 | 213 | // Wait for a short time to allow the committer to run |
213 | 214 | time.Sleep(200 * time.Millisecond) |
214 | 215 | } |
215 | 216 |
|
| 217 | +func TestCommitterRespectsSIGTERM(t *testing.T) { |
| 218 | + mockRPC := mocks.NewMockIRPCClient(t) |
| 219 | + mockMainStorage := mocks.NewMockIMainStorage(t) |
| 220 | + mockStagingStorage := mocks.NewMockIStagingStorage(t) |
| 221 | + mockStorage := storage.IStorage{ |
| 222 | + MainStorage: mockMainStorage, |
| 223 | + StagingStorage: mockStagingStorage, |
| 224 | + } |
| 225 | + |
| 226 | + committer := NewCommitter(mockRPC, mockStorage) |
| 227 | + committer.triggerIntervalMs = 100 // Short interval for testing |
| 228 | + |
| 229 | + chainID := big.NewInt(1) |
| 230 | + mockRPC.EXPECT().GetChainID().Return(chainID) |
| 231 | + mockMainStorage.EXPECT().GetMaxBlockNumber(chainID).Return(big.NewInt(100), nil) |
| 232 | + |
| 233 | + blockData := []common.BlockData{ |
| 234 | + {Block: common.Block{Number: big.NewInt(101)}}, |
| 235 | + {Block: common.Block{Number: big.NewInt(102)}}, |
| 236 | + } |
| 237 | + mockStagingStorage.On("GetStagingData", mock.Anything).Return(&blockData, nil) |
| 238 | + mockMainStorage.On("InsertBlockData", &blockData).Return(nil) |
| 239 | + mockStagingStorage.On("DeleteStagingData", &blockData).Return(nil) |
| 240 | + |
| 241 | + // Create a context that we can cancel |
| 242 | + ctx, cancel := context.WithCancel(context.Background()) |
| 243 | + |
| 244 | + // Start the committer in a goroutine |
| 245 | + done := make(chan struct{}) |
| 246 | + go func() { |
| 247 | + committer.Start(ctx) |
| 248 | + close(done) |
| 249 | + }() |
| 250 | + |
| 251 | + // Wait a bit to ensure the committer is running |
| 252 | + time.Sleep(200 * time.Millisecond) |
| 253 | + |
| 254 | + // Cancel the context (simulating SIGTERM) |
| 255 | + cancel() |
| 256 | + |
| 257 | + // Wait for the committer to stop with a timeout |
| 258 | + select { |
| 259 | + case <-done: |
| 260 | + // Success - committer stopped |
| 261 | + case <-time.After(2 * time.Second): |
| 262 | + t.Fatal("Committer did not stop within timeout period after receiving cancel signal") |
| 263 | + } |
| 264 | +} |
| 265 | + |
216 | 266 | func TestHandleMissingStagingData(t *testing.T) { |
217 | 267 | defer func() { config.Cfg = config.Config{} }() |
218 | 268 | config.Cfg.Committer.BlocksPerCommit = 5 |
|
0 commit comments