forked from evmos/ethermint
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c96f03
commit b276443
Showing
3 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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,75 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
"github.com/stretchr/testify/require" | ||
|
||
coretypes "github.com/cometbft/cometbft/rpc/core/types" | ||
) | ||
|
||
var ( | ||
failedResponse = &coretypes.ResultStatus{ | ||
SyncInfo: coretypes.SyncInfo{ | ||
LatestBlockHeight: 0, | ||
}, | ||
} | ||
|
||
successfulResponse = &coretypes.ResultStatus{ | ||
SyncInfo: coretypes.SyncInfo{ | ||
LatestBlockHeight: 1, | ||
}, | ||
} | ||
) | ||
|
||
type statusClientMock struct { | ||
// retries left before success response | ||
retriesLeft uint | ||
} | ||
|
||
func newStatusClientMock(retriesLeft uint) *statusClientMock { | ||
return &statusClientMock{ | ||
retriesLeft: retriesLeft, | ||
} | ||
} | ||
|
||
func (m *statusClientMock) Status(context.Context) (*coretypes.ResultStatus, error) { | ||
if m.retriesLeft == 0 { | ||
return successfulResponse, nil | ||
} | ||
|
||
m.retriesLeft-- | ||
return failedResponse, nil | ||
} | ||
|
||
func TestWaitUntilClientReady(t *testing.T) { | ||
for _, tc := range []struct { | ||
desc string | ||
retriesLeft uint | ||
}{ | ||
{ | ||
desc: "test case #1", | ||
retriesLeft: 0, | ||
}, | ||
{ | ||
desc: "test case #2", | ||
retriesLeft: 1, | ||
}, | ||
{ | ||
desc: "test case #3", | ||
retriesLeft: 10, | ||
}, | ||
} { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
ctxb := context.Background() | ||
mock := newStatusClientMock(tc.retriesLeft) | ||
|
||
err := waitUntilClientReady(ctxb, mock, backoff.NewConstantBackOff(time.Nanosecond)) | ||
require.NoError(t, err) | ||
require.Equal(t, uint(0), mock.retriesLeft) | ||
}) | ||
} | ||
} |