|
| 1 | +// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package p2ptest |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + "github.com/ava-labs/avalanchego/ids" |
| 14 | + "github.com/ava-labs/avalanchego/network/p2p" |
| 15 | + "github.com/ava-labs/avalanchego/snow/engine/common" |
| 16 | + "github.com/ava-labs/avalanchego/utils/set" |
| 17 | +) |
| 18 | + |
| 19 | +func TestNewClient_AppGossip(t *testing.T) { |
| 20 | + require := require.New(t) |
| 21 | + ctx := context.Background() |
| 22 | + |
| 23 | + appGossipChan := make(chan struct{}) |
| 24 | + testHandler := p2p.TestHandler{ |
| 25 | + AppGossipF: func(context.Context, ids.NodeID, []byte) { |
| 26 | + close(appGossipChan) |
| 27 | + }, |
| 28 | + } |
| 29 | + |
| 30 | + client := NewClient(t, ctx, testHandler) |
| 31 | + require.NoError(client.AppGossip(ctx, common.SendConfig{}, []byte("foobar"))) |
| 32 | + <-appGossipChan |
| 33 | +} |
| 34 | + |
| 35 | +func TestNewClient_AppRequest(t *testing.T) { |
| 36 | + tests := []struct { |
| 37 | + name string |
| 38 | + appResponse []byte |
| 39 | + appErr error |
| 40 | + appRequestF func(ctx context.Context, client *p2p.Client, onResponse p2p.AppResponseCallback) error |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "AppRequest - response", |
| 44 | + appResponse: []byte("foobar"), |
| 45 | + appRequestF: func(ctx context.Context, client *p2p.Client, onResponse p2p.AppResponseCallback) error { |
| 46 | + return client.AppRequest(ctx, set.Of(ids.GenerateTestNodeID()), []byte("foo"), onResponse) |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "AppRequest - error", |
| 51 | + appErr: &common.AppError{ |
| 52 | + Code: 123, |
| 53 | + Message: "foobar", |
| 54 | + }, |
| 55 | + appRequestF: func(ctx context.Context, client *p2p.Client, onResponse p2p.AppResponseCallback) error { |
| 56 | + return client.AppRequest(ctx, set.Of(ids.GenerateTestNodeID()), []byte("foo"), onResponse) |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "AppRequestAny - response", |
| 61 | + appResponse: []byte("foobar"), |
| 62 | + appRequestF: func(ctx context.Context, client *p2p.Client, onResponse p2p.AppResponseCallback) error { |
| 63 | + return client.AppRequestAny(ctx, []byte("foo"), onResponse) |
| 64 | + }, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "AppRequestAny - error", |
| 68 | + appErr: &common.AppError{ |
| 69 | + Code: 123, |
| 70 | + Message: "foobar", |
| 71 | + }, |
| 72 | + appRequestF: func(ctx context.Context, client *p2p.Client, onResponse p2p.AppResponseCallback) error { |
| 73 | + return client.AppRequestAny(ctx, []byte("foo"), onResponse) |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + for _, tt := range tests { |
| 79 | + t.Run(tt.name, func(t *testing.T) { |
| 80 | + require := require.New(t) |
| 81 | + ctx := context.Background() |
| 82 | + |
| 83 | + appRequestChan := make(chan struct{}) |
| 84 | + testHandler := p2p.TestHandler{ |
| 85 | + AppRequestF: func(context.Context, ids.NodeID, time.Time, []byte) ([]byte, *common.AppError) { |
| 86 | + if tt.appErr != nil { |
| 87 | + return nil, &common.AppError{ |
| 88 | + Code: 123, |
| 89 | + Message: tt.appErr.Error(), |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return tt.appResponse, nil |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + client := NewClient(t, ctx, testHandler) |
| 98 | + require.NoError(tt.appRequestF( |
| 99 | + ctx, |
| 100 | + client, |
| 101 | + func(_ context.Context, _ ids.NodeID, responseBytes []byte, err error) { |
| 102 | + require.ErrorIs(err, tt.appErr) |
| 103 | + require.Equal(tt.appResponse, responseBytes) |
| 104 | + close(appRequestChan) |
| 105 | + }, |
| 106 | + )) |
| 107 | + <-appRequestChan |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
0 commit comments