Skip to content

Commit c5b6ac6

Browse files
review fixes
1 parent 2b504fa commit c5b6ac6

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

nc/nc.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (MqttClientBuilder) Build(opts *mqtt.ClientOptions) mqtt.Client {
116116
return mqtt.NewClient(opts)
117117
}
118118

119-
type TimeFunc func(int) time.Duration
119+
type CalculateRetryDelayForAttempt func(attempt int) time.Duration
120120

121121
// Client is a client for Notification center
122122
type Client struct {
@@ -128,7 +128,7 @@ type Client struct {
128128
subjectErr events.Publisher[error]
129129
subjectPeerUpdate events.Publisher[[]string]
130130
credsFetcher CredentialsGetter
131-
timeFunc TimeFunc
131+
retryDelayFunc CalculateRetryDelayForAttempt
132132

133133
startMu sync.Mutex
134134
started bool
@@ -150,7 +150,7 @@ func NewClient(
150150
subjectErr: subjectErr,
151151
subjectPeerUpdate: subjectPeerUpdate,
152152
credsFetcher: credsFetcher,
153-
timeFunc: network.ExponentialBackoff,
153+
retryDelayFunc: network.ExponentialBackoff,
154154
}
155155
}
156156

@@ -246,6 +246,7 @@ func (c *Client) tryConnect(
246246
select {
247247
case managementChan <- credentials.ExpirationDate:
248248
case <-ctx.Done():
249+
return client, connectionState
249250
}
250251

251252
opts := c.createClientOptions(credentials, managementChan, ctx)
@@ -309,7 +310,7 @@ func (c *Client) connectWithBackoff(client mqtt.Client,
309310
client.Disconnect(0)
310311
}
311312
return client
312-
case <-time.After(c.timeFunc(tries)):
313+
case <-time.After(c.retryDelayFunc(tries)):
313314
}
314315
}
315316

nc/nc_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ func TestConnectionCancellation(t *testing.T) {
204204
connectionErr error
205205
fetchCredentialsErr error
206206
tokenTimeout time.Duration // how long client will wait for connection to be established
207+
delayBeforeCancel time.Duration
207208
}{
208209
{
209210
name: "connection success",
@@ -224,6 +225,10 @@ func TestConnectionCancellation(t *testing.T) {
224225
name: "cancel while waiting for connection",
225226
tokenTimeout: 10 * time.Second,
226227
},
228+
{
229+
name: "delay before cancel",
230+
delayBeforeCancel: 10 * time.Millisecond,
231+
},
227232
}
228233

229234
for _, test := range tests {
@@ -248,7 +253,7 @@ func TestConnectionCancellation(t *testing.T) {
248253
subjectErr: &subs.Subject[error]{},
249254
subjectPeerUpdate: &subs.Subject[[]string]{},
250255
credsFetcher: credsFetcher,
251-
timeFunc: func(i int) time.Duration { return test.tokenTimeout },
256+
retryDelayFunc: func(i int) time.Duration { return test.tokenTimeout },
252257
}
253258

254259
t.Run(test.name, func(t *testing.T) {
@@ -259,6 +264,7 @@ func TestConnectionCancellation(t *testing.T) {
259264
connectedChan <- true
260265
}()
261266

267+
time.Sleep(test.delayBeforeCancel)
262268
cancelFunc()
263269

264270
select {

0 commit comments

Comments
 (0)