Skip to content
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

Add a test checking the support for subscribing to log events #369

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions tests/integration_test_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ import (
// integration test networks can also be used for automated integration and
// regression tests for client code.
type IntegrationTestNet struct {
directory string
done <-chan struct{}
validator Account
httpClientPort int
directory string
done <-chan struct{}
validator Account
httpPort int
wsPort int
}

func isPortFree(host string, port int) bool {
Expand Down Expand Up @@ -226,11 +227,11 @@ func (n *IntegrationTestNet) start() error {

// find free ports for the http-client, ws-client, and network interfaces
var err error
n.httpClientPort, err = getFreePort()
n.httpPort, err = getFreePort()
if err != nil {
return err
}
wsPort, err := getFreePort()
n.wsPort, err = getFreePort()
if err != nil {
return err
}
Expand Down Expand Up @@ -258,11 +259,11 @@ func (n *IntegrationTestNet) start() error {
"--fakenet", "1/1",

// http-client option
"--http", "--http.addr", "127.0.0.1", "--http.port", fmt.Sprint(n.httpClientPort),
"--http", "--http.addr", "127.0.0.1", "--http.port", fmt.Sprint(n.httpPort),
"--http.api", "admin,eth,web3,net,txpool,ftm,trace,debug",

// websocket-client options
"--ws", "--ws.addr", "127.0.0.1", "--ws.port", fmt.Sprint(wsPort),
"--ws", "--ws.addr", "127.0.0.1", "--ws.port", fmt.Sprint(n.wsPort),
"--ws.api", "admin,eth,ftm",

// net options
Expand Down Expand Up @@ -470,7 +471,13 @@ func (n *IntegrationTestNet) GetTransactOptions(account *Account) (*bind.Transac
// GetClient provides raw access to a fresh connection to the network.
// The resulting client must be closed after use.
func (n *IntegrationTestNet) GetClient() (*ethclient.Client, error) {
return ethclient.Dial(fmt.Sprintf("http://localhost:%d", n.httpClientPort))
return ethclient.Dial(fmt.Sprintf("http://localhost:%d", n.httpPort))
}

// GetWebSocketClient provides raw access to a fresh connection to the network
// using the WebSocket protocol. The resulting client must be closed after use.
func (n *IntegrationTestNet) GetWebSocketClient() (*ethclient.Client, error) {
return ethclient.Dial(fmt.Sprintf("ws://localhost:%d", n.wsPort))
}

// RestartWithExportImport stops the network, exports the genesis file, cleans the
Expand Down
52 changes: 52 additions & 0 deletions tests/log_subscription_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package tests

import (
"context"
"testing"
"time"

"github.com/Fantom-foundation/go-opera/tests/contracts/counter_event_emitter"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/require"
)

func TestLogSubscription_CanGetCallBacksForLogEvents(t *testing.T) {
const NumEvents = 3
require := require.New(t)
net, err := StartIntegrationTestNet(t.TempDir())
require.NoError(err, "Failed to start the fake network: ", err)
defer net.Stop()

contract, _, err := DeployContract(net, counter_event_emitter.DeployCounterEventEmitter)
require.NoError(err)

client, err := net.GetWebSocketClient()
require.NoError(err, "failed to get client; ", err)
defer client.Close()

allLogs := make(chan types.Log, NumEvents)
subscription, err := client.SubscribeFilterLogs(
context.Background(),
ethereum.FilterQuery{},
allLogs,
)
require.NoError(err, "failed to subscribe to logs; ", err)
defer subscription.Unsubscribe()

for range NumEvents {
_, err = net.Apply(contract.Increment)
require.NoError(err)
}

for i := range NumEvents {
select {
case log := <-allLogs:
event, err := contract.ParseCount(log)
require.NoError(err)
require.Equal(uint64(i+1), event.TotalCount.Uint64())
case <-time.After(5 * time.Second):
require.Fail("expected log event not received")
}
}
}
Loading