Skip to content

Commit

Permalink
simple test to fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pnowosie committed Oct 22, 2024
1 parent e7429e8 commit c5eb695
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions rpc/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,3 +622,60 @@ func TestSubscribeTxStatusAndUnsubscribe(t *testing.T) {
require.Nil(t, rpcErr)
require.True(t, ok)
}

func TestSubscribeTxStatusAndUnsubscribeSimple(t *testing.T) {
t.Parallel()
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)

mockReader := mocks.NewMockReader(mockCtrl)

syncer := newFakeSyncer()
log, _ := utils.NewZapLogger(utils.INFO, false)
handler := rpc.New(mockReader, syncer, nil, "", log)

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

go func() {
require.NoError(t, handler.Run(ctx))
}()
// Technically, there's a race between goroutine above and the SubscribeNewHeads call down below.
// Sleep for a moment just in case.
time.Sleep(50 * time.Millisecond)

serverConn, clientConn := net.Pipe()
t.Cleanup(func() {
require.NoError(t, serverConn.Close())
require.NoError(t, clientConn.Close())
})

txnHash := utils.HexToFelt(t, "0x4c5772d1914fe6ce891b64eb35bf3522aeae1315647314aac58b01137607f3f")
txn := &core.DeployTransaction{TransactionHash: txnHash, Version: (*core.TransactionVersion)(&felt.Zero)}
receipt := &core.TransactionReceipt{
TransactionHash: txnHash,
Reverted: false,
}

mockReader.EXPECT().TransactionByHash(txnHash).Return(txn, nil).Times(1)
mockReader.EXPECT().Receipt(txnHash).Return(receipt, nil, uint64(1), nil).Times(1)
mockReader.EXPECT().TransactionByHash(gomock.Any()).Return(nil, db.ErrKeyNotFound).AnyTimes()

// Subscribe correctly for the known transaction
subCtx := context.WithValue(ctx, jsonrpc.ConnKey{}, &fakeConn{w: serverConn})
id, rpcErr := handler.SubscribeTxnStatus(subCtx, *txnHash, nil)
require.Nil(t, rpcErr)

// Receive a block header.
time.Sleep(100 * time.Millisecond)
got := make([]byte, 0, 300)
_, err := clientConn.Read(got)
require.NoError(t, err)
require.Equal(t, "", string(got))

// Unsubscribe on correct connection with the correct id.
subCtx = context.WithValue(context.Background(), jsonrpc.ConnKey{}, &fakeConn{w: serverConn})
ok, rpcErr := handler.Unsubscribe(subCtx, id.ID)
require.Nil(t, rpcErr)
require.True(t, ok)
}

0 comments on commit c5eb695

Please sign in to comment.