-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Network tests: new heads subscriptions
- Loading branch information
1 parent
52579fe
commit 83cb136
Showing
9 changed files
with
146 additions
and
10 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package actions | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ten-protocol/go-ten/integration/networktest" | ||
"github.com/ten-protocol/go-ten/integration/networktest/userwallet" | ||
) | ||
|
||
type recordNewHeadsSubscriptionAction struct { | ||
duration time.Duration | ||
gatewayUser int // -1 if not using gateway, else test user index to get gateway from | ||
|
||
recordedHeads []*types.Header | ||
} | ||
|
||
func (r *recordNewHeadsSubscriptionAction) Run(ctx context.Context, network networktest.NetworkConnector) (context.Context, error) { | ||
// get gateway address for first user | ||
user, err := FetchTestUser(ctx, r.gatewayUser) | ||
if err != nil { | ||
return ctx, err | ||
} | ||
// verify user is a gateway user | ||
gwUser, ok := user.(*userwallet.GatewayUser) | ||
if !ok { | ||
return ctx, fmt.Errorf("user is not a gateway user") | ||
} | ||
ethClient, err := gwUser.WSClient() | ||
if err != nil { | ||
return ctx, err | ||
} | ||
headsCh := make(chan *types.Header) | ||
sub, err := ethClient.SubscribeNewHead(ctx, headsCh) | ||
if err != nil { | ||
return nil, err | ||
} | ||
startTime := time.Now() | ||
fmt.Println("Listening for new heads") | ||
// read from headsCh for duration or until subscription is closed | ||
for time.Since(startTime) < r.duration { | ||
select { | ||
case head := <-headsCh: | ||
// read and store head from headsCh, then continue listening if duration has not expired | ||
fmt.Printf("Received new head: %v\n", head.Number) | ||
r.recordedHeads = append(r.recordedHeads, head) | ||
case <-time.After(500 * time.Millisecond): | ||
// no new head received, continue listening if duration has not expired | ||
case <-sub.Err(): | ||
// subscription closed | ||
return ctx, fmt.Errorf("subscription closed unexpectedly") | ||
case <-ctx.Done(): | ||
sub.Unsubscribe() | ||
return ctx, fmt.Errorf("context cancelled") | ||
} | ||
} | ||
sub.Unsubscribe() | ||
return ctx, nil | ||
} | ||
|
||
func (r *recordNewHeadsSubscriptionAction) Verify(ctx context.Context, network networktest.NetworkConnector) error { | ||
if len(r.recordedHeads) == 0 { | ||
return fmt.Errorf("no new heads received during the %s period", r.duration) | ||
} | ||
return nil | ||
} | ||
|
||
func RecordNewHeadsSubscription(duration time.Duration) networktest.Action { | ||
// for now this test expects a gateway user and tests via the gateway | ||
// todo: add support for testing without a gateway (need to add newHeads subscription to ObsClient) | ||
return &recordNewHeadsSubscriptionAction{duration: duration, gatewayUser: 0} | ||
} |
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
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
28 changes: 28 additions & 0 deletions
28
integration/networktest/tests/subscription/subscriptions_test.go
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,28 @@ | ||
package subscription | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/ten-protocol/go-ten/integration/networktest" | ||
"github.com/ten-protocol/go-ten/integration/networktest/actions" | ||
"github.com/ten-protocol/go-ten/integration/networktest/env" | ||
) | ||
|
||
// this test goes via the gateway only for now | ||
func TestGatewayNewHeadsSubscription(t *testing.T) { | ||
networktest.TestOnlyRunsInIDE(t) | ||
networktest.Run( | ||
"gateway-new-heads-subscription", | ||
t, | ||
env.UATTestnet(), //env.LocalDevNetwork(devnetwork.WithGateway()), | ||
actions.Series( | ||
// user not technically needed, but we need a gateway address to use | ||
&actions.CreateTestUser{UserID: 0, UseGateway: true}, | ||
actions.SetContextValue(actions.KeyNumberOfTestUsers, 1), | ||
|
||
// Record new heads for 30 seconds, verify that the subscription is working | ||
actions.RecordNewHeadsSubscription(30*time.Second), | ||
), | ||
) | ||
} |
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