From c57bd61ad414758ac5ddcdb2beb4688c3348fd5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flc=E3=82=9B?= Date: Thu, 15 Feb 2024 21:58:33 +0800 Subject: [PATCH] tests(coordinator): Improve unit test coverage (#98) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * tests(coordinator): Improve unit test coverage Signed-off-by: Flc゛ * tests(coordinator): Improve unit test coverage Signed-off-by: Flc゛ --------- Signed-off-by: Flc゛ --- coordinator/manager.go | 2 +- coordinator/manager_test.go | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/coordinator/manager.go b/coordinator/manager.go index 3ce5ff7c..2871bbf0 100644 --- a/coordinator/manager.go +++ b/coordinator/manager.go @@ -4,7 +4,7 @@ import "sync" type Manager struct { coordinators map[string]*Coordinator - mu sync.RWMutex + mu sync.Mutex } func NewManager() *Manager { diff --git a/coordinator/manager_test.go b/coordinator/manager_test.go index 96268c9f..7c0b1c52 100644 --- a/coordinator/manager_test.go +++ b/coordinator/manager_test.go @@ -9,7 +9,7 @@ import ( func TestManager(t *testing.T) { wg := sync.WaitGroup{} - ch := make(chan struct{}, 2) + ch := make(chan struct{}, 10) c1 := Until("foo") c2 := Until("foo") @@ -39,4 +39,30 @@ func TestManager(t *testing.T) { wg.Wait() assert.Equal(t, 2, len(ch)) + + // Clear all coordinators + Clear() + + c3 := Until("foo") + assert.NotSame(t, c1, c3) + + // Close foo coordinators + wg.Add(1) + go func() { + defer wg.Done() + + if <-c3.Done(); true { + ch <- struct{}{} + return + } + }() + assert.Equal(t, 2, len(ch)) + Close("foo") + wg.Wait() + assert.Equal(t, 3, len(ch)) + + // Close non-exist coordinator + assert.NotPanics(t, func() { + Close("bar") + }) }