From e6266d2e054442e07695b35ef9448b4c42fcb0ed Mon Sep 17 00:00:00 2001 From: Benjamin Wenger <ben@elixxir.ioo> Date: Tue, 30 Aug 2022 14:24:58 -0700 Subject: [PATCH] added tests for manager.go --- channels/joinedChannel_test.go | 13 ++++-- channels/manager_test.go | 72 ++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 3 deletions(-) diff --git a/channels/joinedChannel_test.go b/channels/joinedChannel_test.go index b37147979..a82a75884 100644 --- a/channels/joinedChannel_test.go +++ b/channels/joinedChannel_test.go @@ -495,10 +495,17 @@ func (m *mockBroadcastClient) RemoveIdentity(*id.ID) //////////////////////////////////////////////////////////////////////////////// // mockEventModel adheres to the EventModel interface. -type mockEventModel struct{} +type mockEventModel struct { + joinedCh *cryptoBroadcast.Channel + leftCh *id.ID +} -func (m *mockEventModel) JoinChannel(*cryptoBroadcast.Channel) {} -func (m *mockEventModel) LeaveChannel(*id.ID) {} +func (m *mockEventModel) JoinChannel(c *cryptoBroadcast.Channel) { + m.joinedCh = c +} +func (m *mockEventModel) LeaveChannel(c *id.ID) { + m.leftCh = c +} func (m *mockEventModel) ReceiveMessage(*id.ID, cryptoChannel.MessageID, string, string, time.Time, time.Duration, rounds.Round) { } diff --git a/channels/manager_test.go b/channels/manager_test.go index 25a1e11ee..25fba2b86 100644 --- a/channels/manager_test.go +++ b/channels/manager_test.go @@ -1,8 +1,13 @@ package channels import ( + "gitlab.com/elixxir/client/storage/versioned" + "gitlab.com/elixxir/crypto/fastRNG" + "gitlab.com/elixxir/ekv" + "gitlab.com/xx_network/crypto/csprng" "os" "testing" + "time" jww "github.com/spf13/jwalterweatherman" ) @@ -13,3 +18,70 @@ func TestMain(m *testing.M) { jww.SetStdoutThreshold(jww.LevelWarn) os.Exit(m.Run()) } + +func TestManager_JoinChannel(t *testing.T) { + mem := &mockEventModel{} + + m := NewManager(versioned.NewKV(ekv.MakeMemstore()), + new(mockBroadcastClient), + fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG), + new(mockNameService), mem).(*manager) + + ch, _, err := newTestChannel("name", "description", m.rng.GetStream()) + if err != nil { + t.Errorf("Failed to create new channel: %+v", err) + } + + err = m.JoinChannel(ch) + if err != nil { + t.Fatalf("Join Channel Errored: %s", err) + } + + if _, exists := m.channels[*ch.ReceptionID]; !exists { + t.Errorf("Channel %s not added to channel map.", ch.Name) + } + + //wait because the event model is called in another thread + time.Sleep(1 * time.Second) + + if mem.joinedCh == nil { + t.Errorf("the channel join call was not propogated to the event " + + "model") + } +} + +func TestManager_LeaveChannel(t *testing.T) { + mem := &mockEventModel{} + + m := NewManager(versioned.NewKV(ekv.MakeMemstore()), + new(mockBroadcastClient), + fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG), + new(mockNameService), mem).(*manager) + + ch, _, err := newTestChannel("name", "description", m.rng.GetStream()) + if err != nil { + t.Errorf("Failed to create new channel: %+v", err) + } + + err = m.JoinChannel(ch) + if err != nil { + t.Fatalf("Join Channel Errored: %s", err) + } + + err = m.LeaveChannel(ch.ReceptionID) + if err != nil { + t.Fatalf("Leave Channel Errored: %s", err) + } + + if _, exists := m.channels[*ch.ReceptionID]; exists { + t.Errorf("Channel %s still in map.", ch.Name) + } + + //wait because the event model is called in another thread + time.Sleep(1 * time.Second) + + if mem.leftCh == nil { + t.Errorf("the channel join call was not propogated to the event " + + "model") + } +} -- GitLab