|
| 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 TestBlockInfoPublisher(t *testing.T) { |
| 15 | + port := testsuite.FindFreePort() |
| 16 | + addr := fmt.Sprintf("tcp://localhost:%d", port) |
| 17 | + conf := DefaultConfig() |
| 18 | + conf.ZmqPubBlockInfo = 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(TopicBlockInfo.Bytes())) |
| 31 | + require.NoError(t, err) |
| 32 | + |
| 33 | + blk, _ := td.TestSuite.GenerateTestBlock(td.RandHeight()) |
| 34 | + |
| 35 | + td.eventCh <- blk |
| 36 | + |
| 37 | + received, err := sub.Recv() |
| 38 | + require.NoError(t, err) |
| 39 | + |
| 40 | + require.NotNil(t, received.Frames) |
| 41 | + require.GreaterOrEqual(t, len(received.Frames), 1) |
| 42 | + |
| 43 | + msg := received.Frames[0] |
| 44 | + require.Len(t, msg, 37) |
| 45 | + |
| 46 | + topic := msg[:2] |
| 47 | + proposerBytes := msg[2:23] |
| 48 | + timestamp := binary.BigEndian.Uint32(msg[23:27]) |
| 49 | + txCount := binary.BigEndian.Uint16(msg[27:29]) |
| 50 | + height := binary.BigEndian.Uint32(msg[29:33]) |
| 51 | + seqNo := binary.BigEndian.Uint32(msg[33:]) |
| 52 | + |
| 53 | + require.Equal(t, TopicBlockInfo.Bytes(), topic) |
| 54 | + require.Equal(t, blk.Header().ProposerAddress().Bytes(), proposerBytes) |
| 55 | + require.Equal(t, blk.Header().UnixTime(), timestamp) |
| 56 | + require.Equal(t, uint16(len(blk.Transactions())), txCount) |
| 57 | + require.Equal(t, blk.Height(), height) |
| 58 | + require.Equal(t, uint32(0), seqNo) |
| 59 | + |
| 60 | + require.NoError(t, sub.Close()) |
| 61 | +} |
0 commit comments