Skip to content

Commit

Permalink
change 'beforeEach' func to validate ws tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodeev committed Feb 24, 2025
1 parent abfd00e commit 501aece
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 74 deletions.
14 changes: 7 additions & 7 deletions rpc/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
//
// none
func TestBlockNumber(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

blockNumber, err := testConfig.provider.BlockNumber(context.Background())
require.NoError(t, err, "BlockNumber should not return an error")
Expand All @@ -36,7 +36,7 @@ func TestBlockNumber(t *testing.T) {
//
// none
func TestBlockHashAndNumber(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

blockHashAndNumber, err := testConfig.provider.BlockHashAndNumber(context.Background())
require.NoError(t, err, "BlockHashAndNumber should not return an error")
Expand Down Expand Up @@ -75,7 +75,7 @@ func TestBlockHashAndNumber(t *testing.T) {
//
// none
func TestBlockWithTxHashes(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
BlockID BlockID
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestBlockWithTxHashes(t *testing.T) {
//
// none
func TestBlockWithTxs(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)
require := require.New(t)

type testSetType struct {
Expand Down Expand Up @@ -370,7 +370,7 @@ func TestBlockWithTxs(t *testing.T) {
//
// none
func TestBlockTransactionCount(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
BlockID BlockID
Expand Down Expand Up @@ -418,7 +418,7 @@ func TestBlockTransactionCount(t *testing.T) {
//
// none
func TestCaptureUnsupportedBlockTxn(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
StartBlock uint64
Expand Down Expand Up @@ -477,7 +477,7 @@ func TestCaptureUnsupportedBlockTxn(t *testing.T) {
//
// none
func TestStateUpdate(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
BlockID BlockID
Expand Down
2 changes: 1 addition & 1 deletion rpc/call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
//
// none
func TestCall(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
FunctionCall FunctionCall
Expand Down
4 changes: 2 additions & 2 deletions rpc/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
//
// none
func TestChainID(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
ChainID string
Expand Down Expand Up @@ -56,7 +56,7 @@ func TestChainID(t *testing.T) {
//
// none
func TestSyncing(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
ChainID string
Expand Down
14 changes: 7 additions & 7 deletions rpc/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
//
// none
func TestClassAt(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
ContractAddress *felt.Felt
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestClassAt(t *testing.T) {
//
// none
func TestClassHashAt(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
ContractHash *felt.Felt
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestClassHashAt(t *testing.T) {
//
// none
func TestClass(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
BlockID BlockID
Expand Down Expand Up @@ -278,7 +278,7 @@ func TestClass(t *testing.T) {
//
// none
func TestStorageAt(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
ContractHash *felt.Felt
Expand Down Expand Up @@ -342,7 +342,7 @@ func TestStorageAt(t *testing.T) {
//
// none
func TestNonce(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
ContractAddress *felt.Felt
Expand Down Expand Up @@ -398,7 +398,7 @@ func TestNonce(t *testing.T) {
// none
func TestEstimateMessageFee(t *testing.T) {
//TODO: upgrade the testnet test case before merge
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
MsgFromL1
Expand Down Expand Up @@ -484,7 +484,7 @@ func TestEstimateFee(t *testing.T) {
//TODO: upgrade the mainnet and testnet test cases before merge
t.Skip("TODO: create a test case for the new 'CONTRACT_EXECUTION_ERROR' type")

testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
txs []BroadcastTxn
Expand Down
2 changes: 1 addition & 1 deletion rpc/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestRPCError(t *testing.T) {
t.Skip("TODO: test the new RPCData field before merge")
if testEnv == "mock" {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)
_, err := testConfig.provider.ChainID(context.Background())
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion rpc/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
//
// none
func TestEvents(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
eventFilter EventFilter
Expand Down
40 changes: 20 additions & 20 deletions rpc/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ var (
// Requires a Testnet Starknet JSON-RPC compliant node (e.g. Juno)
// (ref: /~https://github.com/NethermindEth/juno)
"testnet": {
base: "https://free-rpc.nethermind.io/sepolia-juno",
// wsBase: "ws://localhost:6061",
base: "https://free-rpc.nethermind.io/sepolia-juno",
wsBase: "ws://localhost:6061",
},
// Requires a Devnet configuration running locally
// (ref: /~https://github.com/0xSpaceShard/starknet-devnet-rs)
Expand Down Expand Up @@ -79,20 +79,21 @@ func TestMain(m *testing.M) {
//
// Parameters:
// - t: The testing.T object for testing purposes
// - isWs: a boolean value to check if the test is for the websocket provider
// Returns:
// - *testConfiguration: a pointer to the testConfiguration struct
func beforeEach(t *testing.T) *testConfiguration {
func beforeEach(t *testing.T, isWs bool) *testConfiguration {
t.Helper()
_ = godotenv.Load(fmt.Sprintf(".env.%s", testEnv), ".env")
testConfig, ok := testConfigurations[testEnv]
if !ok {
t.Fatal("env supports mock, testnet, mainnet, devnet, integration")
t.Fatal("env supports mock, testnet, wsTestnet, mainnet, devnet, integration")
}

if testEnv == "mock" {
testConfig.provider = &Provider{
c: &rpcMock{},
}
return &testConfig
}

base := os.Getenv("HTTP_PROVIDER_URL")
Expand All @@ -102,30 +103,29 @@ func beforeEach(t *testing.T) *testConfiguration {

client, err := NewProvider(testConfig.base)
if err != nil {
t.Fatal("connect should succeed, instead:", err)
t.Fatalf("failed to connect to the %s provider: %v", testConfig.base, err)
}
testConfig.provider = client
t.Cleanup(func() {
testConfig.provider.c.Close()
})

if testEnv == "devnet" {
return &testConfig
}
if isWs {
wsBase := os.Getenv("WS_PROVIDER_URL")
if wsBase != "" {
testConfig.wsBase = wsBase

wsBase := os.Getenv("WS_PROVIDER_URL")
if wsBase != "" {
testConfig.wsBase = wsBase
}

wsClient, err := NewWebsocketProvider(testConfig.wsBase)
if err != nil {
t.Fatalf("failed to connect to the %s websocket provider: %v", testConfig.wsBase, err)
}
testConfig.wsProvider = wsClient
t.Cleanup(func() {
testConfig.wsProvider.c.Close()
})
}
wsClient, err := NewWebsocketProvider(testConfig.wsBase)
if err != nil {
t.Fatal("connect should succeed, instead:", err)
}
testConfig.wsProvider = wsClient
t.Cleanup(func() {
testConfig.wsProvider.c.Close()
})

return &testConfig
}
Expand Down
6 changes: 3 additions & 3 deletions rpc/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
//
// none
func TestTransactionTrace(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

var expectedResp InvokeTxnTrace
expectedrespRaw, err := os.ReadFile("./tests/trace/sepoliaInvokeTrace_0x6a4a9c4f1a530f7d6dd7bba9b71f090a70d1e3bbde80998fde11a08aab8b282.json")
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestTransactionTrace(t *testing.T) {
//
// none
func TestSimulateTransaction(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

var simulateTxIn SimulateTransactionInput
var expectedResp SimulateTransactionOutput
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestSimulateTransaction(t *testing.T) {
//
// none
func TestTraceBlockTransactions(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)
require := require.New(t)

var blockTraceSepolia []Trace
Expand Down
8 changes: 4 additions & 4 deletions rpc/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// Returns:
// none
func TestTransactionByHash(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
TxHash *felt.Felt
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestTransactionByHash(t *testing.T) {
//
// none
func TestTransactionByBlockIdAndIndex(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
BlockID BlockID
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestTransactionByBlockIdAndIndex(t *testing.T) {
}

func TestTransactionReceipt(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
TxnHash *felt.Felt
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestTransactionReceipt(t *testing.T) {
func TestGetTransactionStatus(t *testing.T) {
//TODO: implement a test case to 'failure_reason' before merge

testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
TxnHash *felt.Felt
Expand Down
2 changes: 1 addition & 1 deletion rpc/types_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestBlock_Unmarshal(t *testing.T) {
}

func TestBlockWithReceipts(t *testing.T) {
testConfig := beforeEach(t)
testConfig := beforeEach(t, false)
require := require.New(t)

type testSetType struct {
Expand Down
2 changes: 1 addition & 1 deletion rpc/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// TestSpecVersion tests starknet_specVersion
func TestSpecVersion(t *testing.T) {

testConfig := beforeEach(t)
testConfig := beforeEach(t, false)

type testSetType struct {
ExpectedResp string
Expand Down
28 changes: 5 additions & 23 deletions rpc/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ import (
func TestSubscribeNewHeads(t *testing.T) {
t.Parallel()

if testEnv != "testnet" {
t.Skip("Skipping test as it requires a testnet environment")
}

testConfig := beforeEach(t)
testConfig := beforeEach(t, true)

type testSetType struct {
headers chan *BlockHeader
Expand Down Expand Up @@ -109,11 +105,7 @@ func TestSubscribeNewHeads(t *testing.T) {
func TestSubscribeEvents(t *testing.T) {
t.Parallel()

if testEnv != "testnet" {
t.Skip("Skipping test as it requires a testnet environment")
}

testConfig := beforeEach(t)
testConfig := beforeEach(t, true)

provider := testConfig.provider
blockNumber, err := provider.BlockNumber(context.Background())
Expand Down Expand Up @@ -342,11 +334,8 @@ func TestSubscribeEvents(t *testing.T) {

func TestSubscribeTransactionStatus(t *testing.T) {
t.Parallel()
if testEnv != "testnet" {
t.Skip("Skipping test as it requires a testnet environment")
}

testConfig := beforeEach(t)
testConfig := beforeEach(t, true)

provider := testConfig.provider
blockInterface, err := provider.BlockWithTxHashes(context.Background(), WithBlockTag("latest"))
Expand Down Expand Up @@ -392,11 +381,8 @@ func TestSubscribeTransactionStatus(t *testing.T) {

func TestSubscribePendingTransactions(t *testing.T) {
t.Parallel()
if testEnv != "testnet" {
t.Skip("Skipping test as it requires a testnet environment")
}

testConfig := beforeEach(t)
testConfig := beforeEach(t, true)

type testSetType struct {
pendingTxns chan *SubPendingTxns
Expand Down Expand Up @@ -478,11 +464,7 @@ func TestSubscribePendingTransactions(t *testing.T) {
func TestUnsubscribe(t *testing.T) {
t.Parallel()

if testEnv != "testnet" {
t.Skip("Skipping test as it requires a testnet environment")
}

testConfig := beforeEach(t)
testConfig := beforeEach(t, true)

wsProvider := testConfig.wsProvider

Expand Down
Loading

0 comments on commit 501aece

Please sign in to comment.