git.gammaspectra.live/P2Pool/consensus/v3@v3.8.0/monero/client/zmq/zmq_test.go (about) 1 package zmq_test 2 3 import ( 4 "bytes" 5 "context" 6 "errors" 7 "git.gammaspectra.live/P2Pool/consensus/v3/monero/client/zmq" 8 "git.gammaspectra.live/P2Pool/consensus/v3/p2pool/mempool" 9 "os" 10 "strings" 11 "testing" 12 "time" 13 ) 14 15 func TestJSONFromFrame(t *testing.T) { 16 t.Parallel() 17 18 for _, tc := range []struct { 19 name string 20 input []byte 21 expectedJSON []byte 22 expectedTopic zmq.Topic 23 err string 24 }{ 25 { 26 name: "nil", 27 input: nil, 28 err: "malformed", 29 }, 30 31 { 32 name: "empty", 33 input: []byte{}, 34 err: "malformed", 35 }, 36 37 { 38 name: "unknown-topic", 39 input: []byte(`foobar:[{"foo":"bar"}]`), 40 err: "unknown topic", 41 }, 42 43 { 44 name: "proper w/ known-topic", 45 input: []byte(`json-minimal-txpool_add:[{"foo":"bar"}]`), 46 expectedTopic: zmq.TopicMinimalTxPoolAdd, 47 expectedJSON: []byte(`[{"foo":"bar"}]`), 48 }, 49 } { 50 tc := tc 51 52 t.Run(tc.name, func(t *testing.T) { 53 t.Parallel() 54 55 aTopic, aJSON, err := zmq.JSONFromFrame(tc.input) 56 if tc.err != "" { 57 if err == nil { 58 t.Fatal("expected error") 59 } 60 if !strings.Contains(err.Error(), tc.err) { 61 t.Errorf("expected %s in, got %s", tc.err, err.Error()) 62 } 63 return 64 } 65 66 if err != nil { 67 t.Errorf("expected no error, got %s", err) 68 } 69 70 if tc.expectedTopic != aTopic { 71 t.Errorf("expected %s, got %s", tc.expectedTopic, aTopic) 72 } 73 74 if bytes.Compare(tc.expectedJSON, aJSON) != 0 { 75 t.Errorf("expected %s, got %s", string(tc.expectedJSON), string(aJSON)) 76 } 77 }) 78 } 79 } 80 81 func TestClient(t *testing.T) { 82 client := zmq.NewClient(os.Getenv("MONEROD_ZMQ_URL"), zmq.TopicFullChainMain, zmq.TopicFullTxPoolAdd, zmq.TopicFullMinerData, zmq.TopicMinimalChainMain, zmq.TopicMinimalTxPoolAdd) 83 ctx, ctxFunc := context.WithTimeout(context.Background(), time.Second*10) 84 defer ctxFunc() 85 err := client.Listen(ctx, func(chainMain *zmq.FullChainMain) { 86 t.Log(chainMain) 87 }, func(txs []zmq.FullTxPoolAdd) { 88 t.Log(txs) 89 }, func(main *zmq.FullMinerData) { 90 t.Log(main) 91 }, func(chainMain *zmq.MinimalChainMain) { 92 t.Log(chainMain) 93 }, func(txs mempool.Mempool) { 94 95 t.Log(txs) 96 }) 97 if !errors.Is(err, context.DeadlineExceeded) { 98 t.Fatal(err) 99 } 100 }