Skip to content

Commit 0e6c9c3

Browse files
resolve comments
Signed-off-by: Said Altury <Said.Altury@ibm.com>
1 parent 62f12e0 commit 0e6c9c3

File tree

2 files changed

+29
-42
lines changed

2 files changed

+29
-42
lines changed

platform/fabricx/core/finality/provider.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ func NewListenerManagerProvider(fnsp *fabric.NetworkServiceProvider, configProvi
4141
fnsp: fnsp,
4242
configProvider: configProvider,
4343
managers: make(map[string]ListenerManager),
44-
initOnce: sync.Once{},
4544
newNotificationManager: newNotifi,
4645
// Note: baseCtx and initOnce will be initialized in the Initialize method.
4746
}

platform/fabricx/core/finality/provider_test.go

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"errors"
1212
"reflect"
1313
"sync"
14-
"sync/atomic"
1514
"testing"
1615
"time"
1716

@@ -70,23 +69,22 @@ func TestProvider_Initialize(t *testing.T) {
7069
ctx := t.Context()
7170
provider.Initialize(ctx)
7271

73-
require.NotNil(t, provider.baseCtx, "baseCtx should be set after Initialize")
74-
assert.Same(t, ctx, provider.baseCtx, "baseCtx should match the provided context")
72+
require.Same(t, ctx, provider.baseCtx, "baseCtx should match the provided context")
7573
})
7674

7775
t.Run("Initialize_Only_Once_Ignores_Subsequent_Calls", func(t *testing.T) {
7876
t.Parallel()
7977
provider := NewListenerManagerProvider(nil, nil)
8078

81-
ctx1 := t.Context() //context.WithValue(context.Background(), "key", "value1")
82-
ctx2, cancel2 := context.WithCancel(context.Background()) //context.WithValue(context.Background(), "key", "value2")
79+
ctx1 := t.Context()
80+
ctx2, cancel2 := context.WithCancel(context.Background())
8381
defer cancel2()
8482

8583
provider.Initialize(ctx1)
8684
provider.Initialize(ctx2) // Should be ignored due to sync.Once
8785

88-
assert.Same(t, ctx1, provider.baseCtx, "baseCtx should remain as first initialized value")
89-
assert.NotSame(t, ctx2, provider.baseCtx, "Second Initialize call should be ignored")
86+
require.Same(t, ctx1, provider.baseCtx, "baseCtx should remain as first initialized value")
87+
require.NotSame(t, ctx2, provider.baseCtx, "Second Initialize call should be ignored")
9088
})
9189

9290
t.Run("Thread_Safe_Concurrent_Initialize", func(t *testing.T) {
@@ -115,7 +113,7 @@ func TestProvider_NewManager(t *testing.T) {
115113
provider := NewListenerManagerProvider(nil, nil)
116114

117115
// Don't call Initialize
118-
assert.Panics(t, func() {
116+
require.Panics(t, func() {
119117
_, _ = provider.NewManager("network1", "channel1")
120118
}, "Should panic when Provider is not initialized")
121119
})
@@ -158,18 +156,15 @@ func TestProvider_NewManager(t *testing.T) {
158156
storedManager := provider.managers["network1:channel1"]
159157
provider.managersMu.Unlock()
160158

161-
assert.Equal(t, manager, storedManager, "Manager should be stored in map with correct key")
159+
require.Same(t, manager, storedManager, "Manager should be stored in map with correct key")
162160
})
163161

164162
t.Run("Returns_Existing_Manager_Singleton_Pattern", func(t *testing.T) {
165163
t.Parallel()
166164
provider := NewListenerManagerProvider(nil, nil)
167165

168-
var createCount atomic.Int32
169-
170166
// Mock with stream that blocks
171167
provider.newNotificationManager = func(network string, fnsp *fabric.NetworkServiceProvider, cp config.Provider) (*notificationListenerManager, error) {
172-
createCount.Add(1)
173168
return setupMockNotificationManager(t, func(ctx context.Context, stream *mock2.FakeNotifier_OpenNotificationStreamClient) {
174169
stream.RecvStub = func() (*protonotify.NotificationResponse, error) {
175170
<-ctx.Done()
@@ -192,18 +187,15 @@ func TestProvider_NewManager(t *testing.T) {
192187
require.NoError(t, err2)
193188

194189
// Should be the exact same instance
195-
assert.Equal(t, manager1, manager2, "Should return same manager instance for same network:channel")
196-
assert.Equal(t, int32(1), createCount.Load(), "Should only create manager once")
190+
require.Same(t, manager1, manager2, "Should return same manager instance for same network:channel")
191+
require.Equal(t, 1, len(provider.managers), "Should only create manager once")
197192
})
198193

199194
t.Run("Different_Network_Channel_Get_Different_Managers", func(t *testing.T) {
200195
t.Parallel()
201196
provider := NewListenerManagerProvider(nil, nil)
202197

203-
var createCount atomic.Int32
204-
205198
provider.newNotificationManager = func(network string, fnsp *fabric.NetworkServiceProvider, cp config.Provider) (*notificationListenerManager, error) {
206-
createCount.Add(1)
207199
return setupMockNotificationManager(t, func(ctx context.Context, stream *mock2.FakeNotifier_OpenNotificationStreamClient) {
208200
stream.RecvStub = func() (*protonotify.NotificationResponse, error) {
209201
<-ctx.Done()
@@ -225,11 +217,11 @@ func TestProvider_NewManager(t *testing.T) {
225217
require.NoError(t, err3)
226218

227219
// All should be different instances
228-
assert.NotSame(t, manager1, manager2, "Different channels should have different managers")
229-
assert.NotSame(t, manager1, manager3, "Different networks should have different managers")
230-
assert.NotSame(t, manager2, manager3, "Different combinations should have different managers")
231-
232-
assert.Equal(t, int32(3), createCount.Load(), "Should create 3 different managers")
220+
require.NotSame(t, manager1, manager2, "Different channels should have different managers")
221+
require.NotSame(t, manager1, manager3, "Different networks should have different managers")
222+
require.NotSame(t, manager2, manager3, "Different combinations should have different managers")
223+
224+
require.Equal(t, 3, len(provider.managers), "Should create 3 different managers")
233225
})
234226

235227
t.Run("Manager_Removed_When_Listen_Exits_With_Error", func(t *testing.T) {
@@ -258,11 +250,11 @@ func TestProvider_NewManager(t *testing.T) {
258250
<-listenStarted
259251

260252
// Wait for cleanup - the REAL listen() will exit and trigger cleanup
261-
require.Eventually(t, func() bool {
253+
require.EventuallyWithT(t, func(collect *assert.CollectT) {
262254
provider.managersMu.Lock()
263255
defer provider.managersMu.Unlock()
264256
_, exists := provider.managers["network1:channel1"]
265-
return !exists
257+
require.False(collect, exists, "Manager should be removed from map")
266258
}, managerTimeout, managerTick, "Manager should be removed after listen() exits with error")
267259
})
268260

@@ -296,11 +288,11 @@ func TestProvider_NewManager(t *testing.T) {
296288
cancel()
297289

298290
// The REAL listen() will handle context cancellation and exit gracefully
299-
require.Eventually(t, func() bool {
291+
require.EventuallyWithT(t, func(collect *assert.CollectT) {
300292
provider.managersMu.Lock()
301293
defer provider.managersMu.Unlock()
302294
_, exists := provider.managers["network1:channel1"]
303-
return !exists
295+
require.False(collect, exists, "Manager should be removed from map")
304296
}, managerTimeout, managerTick, "Manager should be removed after context cancellation")
305297
})
306298

@@ -321,25 +313,22 @@ func TestProvider_NewManager(t *testing.T) {
321313
manager, err := provider.NewManager("network1", "channel1")
322314

323315
require.Error(t, err, "Should return error from newNotificationManager")
324-
assert.Nil(t, manager, "Manager should be nil on error")
325-
assert.Equal(t, expectedErr, err, "Should return the exact error")
316+
require.Nil(t, manager, "Manager should be nil on error")
317+
require.Equal(t, expectedErr, err, "Should return the exact error")
326318

327319
// Manager should not be stored
328320
provider.managersMu.Lock()
329321
_, exists := provider.managers["network1:channel1"]
330322
provider.managersMu.Unlock()
331323

332-
assert.False(t, exists, "Failed manager should not be stored")
324+
require.False(t, exists, "Failed manager should not be stored")
333325
})
334326

335327
t.Run("Concurrent_NewManager_Calls_Same_Key", func(t *testing.T) {
336328
t.Parallel()
337329
provider := NewListenerManagerProvider(nil, nil)
338330

339-
var createCount atomic.Int32
340-
341331
provider.newNotificationManager = func(network string, fnsp *fabric.NetworkServiceProvider, cp config.Provider) (*notificationListenerManager, error) {
342-
createCount.Add(1)
343332
return setupMockNotificationManager(t, func(ctx context.Context, stream *mock2.FakeNotifier_OpenNotificationStreamClient) {
344333
stream.RecvStub = func() (*protonotify.NotificationResponse, error) {
345334
<-ctx.Done()
@@ -369,12 +358,11 @@ func TestProvider_NewManager(t *testing.T) {
369358

370359
// All should be the same instance
371360
for i := 1; i < numGoroutines; i++ {
372-
assert.Equal(t, managers[0], managers[i], "All concurrent calls should get same manager")
361+
require.Equal(t, managers[0], managers[i], "All concurrent calls should get same manager")
373362
}
374363

375364
// Should only create manager once
376-
count := createCount.Load()
377-
assert.Equal(t, count, int32(1), "Should create manager at most twice even with concurrent access")
365+
require.Equal(t, len(provider.managers), 1, "Should create manager only once even with concurrent access")
378366
})
379367
}
380368

@@ -407,9 +395,9 @@ func TestGetListenerManager(t *testing.T) {
407395
require.NotNil(t, manager, "Manager should not be nil")
408396

409397
// Verify GetService was called with correct type
410-
assert.Equal(t, 1, mockSP.GetServiceCallCount())
398+
require.Equal(t, 1, mockSP.GetServiceCallCount())
411399
serviceType := mockSP.GetServiceArgsForCall(0)
412-
assert.Equal(t, reflect.TypeOf((*ListenerManagerProvider)(nil)), serviceType)
400+
require.Equal(t, reflect.TypeOf((*ListenerManagerProvider)(nil)), serviceType)
413401
})
414402

415403
t.Run("Returns_Error_When_Service_Not_Found", func(t *testing.T) {
@@ -425,8 +413,8 @@ func TestGetListenerManager(t *testing.T) {
425413
manager, err := GetListenerManager(mockSP, "network1", "channel1")
426414

427415
require.Error(t, err, "Should return error when service not found")
428-
assert.Nil(t, manager, "Manager should be nil on error")
429-
assert.Contains(t, err.Error(), "could not find provider")
416+
require.Nil(t, manager, "Manager should be nil on error")
417+
require.Contains(t, err.Error(), "could not find provider")
430418
})
431419

432420
t.Run("Returns_Error_When_NewManager_Fails", func(t *testing.T) {
@@ -451,7 +439,7 @@ func TestGetListenerManager(t *testing.T) {
451439
manager, err := GetListenerManager(mockSP, "network1", "channel1")
452440

453441
require.Error(t, err, "Should return error from NewManager")
454-
assert.Nil(t, manager)
455-
assert.Equal(t, expectedErr, err)
442+
require.Nil(t, manager)
443+
require.Equal(t, expectedErr, err)
456444
})
457445
}

0 commit comments

Comments
 (0)