From 9f504b115040e640b54ebc6c8a196e1913359155 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Wed, 10 Nov 2021 10:46:11 +0100 Subject: [PATCH] test: extend coverage with missing test --- waku/v2/protocol/envelope_test.go | 31 ++++++++++++++++ waku/v2/protocol/filter/filter_map_test.go | 32 ++++++++++++++++ .../lightpush/waku_lightpush_option_test.go | 37 +++++++++++++++++++ waku/v2/protocol/relay/subscription_test.go | 18 +++++++++ waku/v2/utils/time_test.go | 4 +- 5 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 waku/v2/protocol/envelope_test.go create mode 100644 waku/v2/protocol/filter/filter_map_test.go create mode 100644 waku/v2/protocol/lightpush/waku_lightpush_option_test.go create mode 100644 waku/v2/protocol/relay/subscription_test.go diff --git a/waku/v2/protocol/envelope_test.go b/waku/v2/protocol/envelope_test.go new file mode 100644 index 00000000..29d87eb3 --- /dev/null +++ b/waku/v2/protocol/envelope_test.go @@ -0,0 +1,31 @@ +package protocol + +import ( + "testing" + + "github.com/status-im/go-waku/waku/v2/protocol/pb" + "github.com/stretchr/testify/require" +) + +func TestEnvelope(t *testing.T) { + e := NewEnvelope( + &pb.WakuMessage{ContentTopic: "ContentTopic"}, + "test", + ) + + msg := e.Message() + require.Equal(t, "ContentTopic", msg.ContentTopic) + + topic := e.PubsubTopic() + require.Equal(t, "test", topic) + + hash := e.Hash() + require.Equal( + t, + []uint8([]byte{0x2d, 0x84, 0x3d, 0x43, 0x77, 0x14, 0x4, 0xad, 0x64, 0x9d, 0x90, 0xd6, 0x5c, 0xc7, 0x3d, 0x8f, 0x21, 0x49, 0xa, 0xf1, 0x9c, 0x83, 0x88, 0x76, 0x51, 0xba, 0x6f, 0x34, 0x14, 0x78, 0x93, 0xd2}), + hash, + ) + + size := e.Size() + require.Equal(t, 14, size) +} diff --git a/waku/v2/protocol/filter/filter_map_test.go b/waku/v2/protocol/filter/filter_map_test.go new file mode 100644 index 00000000..14636d70 --- /dev/null +++ b/waku/v2/protocol/filter/filter_map_test.go @@ -0,0 +1,32 @@ +package filter + +import ( + "testing" + + "github.com/status-im/go-waku/waku/v2/protocol" + "github.com/stretchr/testify/require" +) + +func TestFilterMap(t *testing.T) { + fmap := NewFilterMap() + + filter := Filter{ + PeerID: "id", + Topic: "test", + ContentFilters: []string{"test"}, + Chan: make(chan *protocol.Envelope), + } + + fmap.Set("test", filter) + res := <-fmap.Items() + require.Equal(t, "test", res.Key) + + item, ok := fmap.Get("test") + require.True(t, ok) + require.Equal(t, "test", item.Topic) + + fmap.Delete("test") + + _, ok = fmap.Get("test") + require.False(t, ok) +} diff --git a/waku/v2/protocol/lightpush/waku_lightpush_option_test.go b/waku/v2/protocol/lightpush/waku_lightpush_option_test.go new file mode 100644 index 00000000..98fdf3c0 --- /dev/null +++ b/waku/v2/protocol/lightpush/waku_lightpush_option_test.go @@ -0,0 +1,37 @@ +package lightpush + +import ( + "context" + "crypto/rand" + "testing" + + "github.com/status-im/go-waku/tests" + "github.com/stretchr/testify/require" +) + +func TestLightPushOption(t *testing.T) { + port, err := tests.FindFreePort(t, "", 5) + require.NoError(t, err) + + host, err := tests.MakeHost(context.Background(), port, rand.Reader) + require.NoError(t, err) + + options := []LightPushOption{ + WithPeer("QmWLxGxG65CZ7vRj5oNXCJvbY9WkF9d9FxuJg8cg8Y7q3"), + WithAutomaticPeerSelection(host), + WithFastestPeerSelection(context.Background()), + WithRequestId([]byte("requestId")), + WithAutomaticRequestId(), + } + + params := new(LightPushParameters) + params.host = host + + for _, opt := range options { + opt(params) + } + + require.Equal(t, host, params.host) + require.NotNil(t, params.selectedPeer) + require.NotNil(t, params.requestId) +} diff --git a/waku/v2/protocol/relay/subscription_test.go b/waku/v2/protocol/relay/subscription_test.go new file mode 100644 index 00000000..3abbdfae --- /dev/null +++ b/waku/v2/protocol/relay/subscription_test.go @@ -0,0 +1,18 @@ +package relay + +import ( + "testing" + + waku_proto "github.com/status-im/go-waku/waku/v2/protocol" + "github.com/stretchr/testify/require" +) + +func TestSubscription(t *testing.T) { + e := Subscription{ + closed: false, + C: make(chan *waku_proto.Envelope, 10), + quit: make(chan struct{}), + } + e.Unsubscribe() + require.True(t, e.closed) +} diff --git a/waku/v2/utils/time_test.go b/waku/v2/utils/time_test.go index fed73f76..26bd7549 100644 --- a/waku/v2/utils/time_test.go +++ b/waku/v2/utils/time_test.go @@ -10,6 +10,8 @@ import ( func TestGetUnixEpochFrom(t *testing.T) { loc := time.UTC timestamp := GetUnixEpochFrom(time.Date(2019, 1, 1, 0, 0, 0, 0, loc)) - require.Equal(t, float64(1546300800), timestamp) + + timestamp = GetUnixEpoch() + require.NotNil(t, timestamp) }