-
Notifications
You must be signed in to change notification settings - Fork 17
created generic sync monitor #587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
blockchainluffy
wants to merge
9
commits into
main
Choose a base branch
from
feat/generic-sync-monitor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
79dbc5a
feat: created generic syncmonitor package in medley and updated gnosi…
blockchainluffy 97f317d
fix: generic sync monitor | lint fixes
blockchainluffy c15341a
moved sync monitor to keyper core from medley
blockchainluffy 02802f9
update sync monitor to not check for dkg
blockchainluffy 6c26b66
merge main to generic sync monitor
blockchainluffy e85a52b
remove dbpool from generic sync monitor
blockchainluffy 96d4a9f
generic sync monitor to handle reorgs
blockchainluffy 242a638
update debug log for sync monitor to include last block number
blockchainluffy 2e9335c
merge main: resolve conflicts
blockchainluffy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
package syncmonitor | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/jackc/pgx/v4" | ||
"gotest.tools/assert" | ||
|
||
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/service" | ||
) | ||
|
||
// MockSyncState is a mock implementation of BlockSyncState for testing. | ||
type MockSyncState struct { | ||
mu sync.Mutex | ||
blockNumber int64 | ||
err error | ||
} | ||
|
||
func (m *MockSyncState) GetSyncedBlockNumber(_ context.Context) (int64, error) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
return m.blockNumber, m.err | ||
} | ||
|
||
func (m *MockSyncState) SetBlockNumber(n int64) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
m.blockNumber = n | ||
} | ||
|
||
func TestSyncMonitor_ThrowsErrorWhenBlockNotIncreasing(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
initialBlockNumber := int64(100) | ||
mockSyncState := &MockSyncState{ | ||
blockNumber: initialBlockNumber, | ||
} | ||
|
||
monitor := &SyncMonitor{ | ||
CheckInterval: 5 * time.Second, | ||
SyncState: mockSyncState, | ||
} | ||
|
||
errCh := make(chan error, 1) | ||
go func() { | ||
err := service.RunWithSighandler(ctx, monitor) | ||
if err != nil { | ||
errCh <- err | ||
} | ||
}() | ||
|
||
time.Sleep(12 * time.Second) | ||
|
||
select { | ||
case err := <-errCh: | ||
assert.ErrorContains(t, err, ErrBlockNotIncreasing.Error()) | ||
case <-time.After(5 * time.Second): | ||
t.Fatal("expected an error, but none was returned") | ||
} | ||
|
||
// Verify final state | ||
finalBlockNumber, err := mockSyncState.GetSyncedBlockNumber(ctx) | ||
assert.NilError(t, err) | ||
assert.Equal(t, initialBlockNumber, finalBlockNumber) | ||
} | ||
|
||
func TestSyncMonitor_HandlesBlockNumberIncreasing(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
initialBlockNumber := int64(100) | ||
mockSyncState := &MockSyncState{ | ||
blockNumber: initialBlockNumber, | ||
} | ||
|
||
monitor := &SyncMonitor{ | ||
CheckInterval: 200 * time.Millisecond, | ||
SyncState: mockSyncState, | ||
} | ||
|
||
monitorCtx, cancelMonitor := context.WithCancel(ctx) | ||
errCh := make(chan error, 1) | ||
go func() { | ||
if err := service.RunWithSighandler(monitorCtx, monitor); err != nil { | ||
errCh <- err | ||
} | ||
}() | ||
|
||
// Update block numbers more quickly | ||
for i := 0; i < 5; i++ { | ||
time.Sleep(200 * time.Millisecond) | ||
mockSyncState.SetBlockNumber(initialBlockNumber + int64(i+1)) | ||
} | ||
|
||
cancelMonitor() | ||
|
||
// Verify final state | ||
finalBlockNumber, err := mockSyncState.GetSyncedBlockNumber(ctx) | ||
assert.NilError(t, err) | ||
assert.Equal(t, initialBlockNumber+5, finalBlockNumber, "block number should have been incremented correctly") | ||
} | ||
|
||
func TestSyncMonitor_RunsNormallyWhenNoEons(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
initialBlockNumber := int64(100) | ||
mockSyncState := &MockSyncState{ | ||
blockNumber: initialBlockNumber, | ||
} | ||
|
||
monitor := &SyncMonitor{ | ||
CheckInterval: 5 * time.Second, | ||
SyncState: mockSyncState, | ||
} | ||
|
||
monitorCtx, cancelMonitor := context.WithCancel(ctx) | ||
defer cancelMonitor() | ||
|
||
errCh := make(chan error, 1) | ||
go func() { | ||
err := service.RunWithSighandler(monitorCtx, monitor) | ||
if err != nil { | ||
errCh <- err | ||
} | ||
}() | ||
|
||
// Let it run for a while without incrementing the block number | ||
time.Sleep(15 * time.Second) | ||
cancelMonitor() | ||
|
||
select { | ||
case err := <-errCh: | ||
assert.ErrorContains(t, err, ErrBlockNotIncreasing.Error()) | ||
case <-time.After(1 * time.Second): | ||
t.Fatalf("expected monitor to throw error, but no error returned") | ||
} | ||
} | ||
|
||
func TestSyncMonitor_ContinuesWhenNoRows(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
// Set up mock sync state that returns no rows error | ||
mockSyncState := &MockSyncState{ | ||
err: pgx.ErrNoRows, | ||
} | ||
mockSyncState.SetBlockNumber(0) // Initialize block number | ||
|
||
monitor := &SyncMonitor{ | ||
CheckInterval: 5 * time.Second, | ||
SyncState: mockSyncState, | ||
} | ||
|
||
monitorCtx, cancelMonitor := context.WithCancel(ctx) | ||
defer cancelMonitor() | ||
|
||
errCh := make(chan error, 1) | ||
go func() { | ||
err := service.RunWithSighandler(monitorCtx, monitor) | ||
if err != nil { | ||
errCh <- err | ||
} | ||
}() | ||
|
||
time.Sleep(15 * time.Second) | ||
cancelMonitor() | ||
|
||
select { | ||
case err := <-errCh: | ||
t.Fatalf("expected monitor to continue without error, but got: %v", err) | ||
case <-time.After(1 * time.Second): | ||
// Test passes if no error is received | ||
} | ||
} | ||
|
||
func TestSyncMonitor_HandlesReorg(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
// Set up mock sync state that returns no rows error | ||
mockSyncState := &MockSyncState{} | ||
mockSyncState.SetBlockNumber(0) // Initialize block number | ||
|
||
monitor := &SyncMonitor{ | ||
CheckInterval: 5 * time.Second, | ||
SyncState: mockSyncState, | ||
} | ||
|
||
monitorCtx, cancelMonitor := context.WithCancel(ctx) | ||
defer cancelMonitor() | ||
|
||
errCh := make(chan error, 1) | ||
go func() { | ||
err := service.RunWithSighandler(monitorCtx, monitor) | ||
if err != nil { | ||
errCh <- err | ||
} | ||
}() | ||
|
||
// Decrease the block number | ||
decreasedBlockNumber := int64(50) | ||
mockSyncState.SetBlockNumber(decreasedBlockNumber) | ||
|
||
time.Sleep(4 * time.Second) | ||
cancelMonitor() | ||
|
||
select { | ||
case err := <-errCh: | ||
t.Fatalf("expected monitor to continue without error, but got: %v", err) | ||
case <-time.After(1 * time.Second): | ||
} | ||
|
||
// Verify the block number was updated to the latest value | ||
syncedData, err := mockSyncState.GetSyncedBlockNumber(ctx) | ||
assert.NilError(t, err) | ||
assert.Equal(t, decreasedBlockNumber, syncedData, "block number should be updated to the decreased value") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.