2019-01-15 09:21:33 +00:00
|
|
|
package shhext
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-11-21 16:19:22 +00:00
|
|
|
whispertypes "github.com/status-im/status-go/protocol/transport/whisper/types"
|
|
|
|
protocol "github.com/status-im/status-go/protocol/types"
|
2019-01-15 09:21:33 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRegisterSameRequests(t *testing.T) {
|
|
|
|
registry := NewRequestsRegistry(10 * time.Second)
|
2019-10-09 14:22:53 +00:00
|
|
|
topics := []whispertypes.TopicType{{1}}
|
2019-11-21 16:19:22 +00:00
|
|
|
require.NoError(t, registry.Register(protocol.Hash{1}, topics))
|
|
|
|
require.Error(t, registry.Register(protocol.Hash{2}, topics))
|
2019-01-15 09:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegisterSameRequestsWithoutDelay(t *testing.T) {
|
|
|
|
registry := NewRequestsRegistry(0)
|
2019-10-09 14:22:53 +00:00
|
|
|
topics := []whispertypes.TopicType{{1}}
|
2019-11-21 16:19:22 +00:00
|
|
|
require.NoError(t, registry.Register(protocol.Hash{1}, topics))
|
|
|
|
require.NoError(t, registry.Register(protocol.Hash{2}, topics))
|
2019-01-15 09:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegisterDifferentRequests(t *testing.T) {
|
|
|
|
registry := NewRequestsRegistry(10 * time.Second)
|
2019-11-21 16:19:22 +00:00
|
|
|
require.NoError(t, registry.Register(protocol.Hash{1}, []whispertypes.TopicType{{1}}))
|
|
|
|
require.NoError(t, registry.Register(protocol.Hash{2}, []whispertypes.TopicType{{2}}))
|
2019-01-15 09:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnregisterReplacedRequest(t *testing.T) {
|
|
|
|
registry := NewRequestsRegistry(0)
|
2019-11-21 16:19:22 +00:00
|
|
|
unreg := protocol.Hash{1}
|
2019-10-09 14:22:53 +00:00
|
|
|
topics := []whispertypes.TopicType{{1}}
|
2019-01-15 09:21:33 +00:00
|
|
|
require.NoError(t, registry.Register(unreg, topics))
|
2019-11-21 16:19:22 +00:00
|
|
|
replacement := protocol.Hash{2}
|
2019-01-15 09:21:33 +00:00
|
|
|
require.NoError(t, registry.Register(replacement, topics))
|
2019-11-21 16:19:22 +00:00
|
|
|
// record should be replaced with protocol.Hash{2}, so when we will remove unreg it will not affect topics map
|
2019-01-15 09:21:33 +00:00
|
|
|
registry.Unregister(unreg)
|
|
|
|
record, exist := registry.uidToTopics[replacement]
|
|
|
|
require.True(t, exist, "replaced record should exist")
|
|
|
|
require.Equal(t, replacement, registry.byTopicsHash[record].lastUID)
|
|
|
|
}
|