|
| 1 | +package zmq |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/binary" |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/go-zeromq/zmq4" |
| 10 | + "github.com/pactus-project/pactus/util/testsuite" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestTxInfoPublisher(t *testing.T) { |
| 15 | + port := testsuite.FindFreePort() |
| 16 | + addr := fmt.Sprintf("tcp://localhost:%d", port) |
| 17 | + conf := DefaultConfig() |
| 18 | + conf.ZmqPubTxInfo = addr |
| 19 | + |
| 20 | + td := setup(t, conf) |
| 21 | + defer td.closeServer() |
| 22 | + |
| 23 | + td.server.Publishers() |
| 24 | + |
| 25 | + sub := zmq4.NewSub(context.TODO(), zmq4.WithAutomaticReconnect(false)) |
| 26 | + |
| 27 | + err := sub.Dial(addr) |
| 28 | + require.NoError(t, err) |
| 29 | + |
| 30 | + err = sub.SetOption(zmq4.OptionSubscribe, string(TopicTransactionInfo.Bytes())) |
| 31 | + require.NoError(t, err) |
| 32 | + |
| 33 | + blk, _ := td.TestSuite.GenerateTestBlock(td.RandHeight()) |
| 34 | + |
| 35 | + td.eventCh <- blk |
| 36 | + |
| 37 | + for i := 0; i < len(blk.Transactions()); i++ { |
| 38 | + received, err := sub.Recv() |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + require.NotNil(t, received.Frames) |
| 42 | + require.GreaterOrEqual(t, len(received.Frames), 1) |
| 43 | + |
| 44 | + msg := received.Frames[0] |
| 45 | + require.Len(t, msg, 42) |
| 46 | + |
| 47 | + topic := msg[:2] |
| 48 | + txHash := msg[2:34] |
| 49 | + height := binary.BigEndian.Uint32(msg[34:38]) |
| 50 | + seqNo := binary.BigEndian.Uint32(msg[38:]) |
| 51 | + |
| 52 | + require.Equal(t, TopicTransactionInfo.Bytes(), topic) |
| 53 | + require.Equal(t, blk.Transactions()[i].ID().Bytes(), txHash) |
| 54 | + require.Equal(t, blk.Height(), height) |
| 55 | + require.Equal(t, uint32(i), seqNo) |
| 56 | + } |
| 57 | + |
| 58 | + require.NoError(t, sub.Close()) |
| 59 | +} |
0 commit comments