Skip to content

Commit 9bf27b7

Browse files
committed
Removed unused receive methods from channel
1 parent fcbcd0b commit 9bf27b7

File tree

2 files changed

+60
-160
lines changed

2 files changed

+60
-160
lines changed

channel.go

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@ type Sender interface {
6464
ResponseCommandSender
6565
}
6666

67-
// ChannelModule defines a proxy interface for executing actions to the envelope channels.
68-
type ChannelModule interface {
69-
StateChanged(ctx context.Context, state SessionState) // StateChanged is called when the session state is changed.
70-
Receiving(ctx context.Context, env envelope) envelope // Receiving is called when an envelope is being received by the channel.
71-
Sending(ctx context.Context, env envelope) envelope // Sending is called when an envelope is being sent by the channel.
72-
}
73-
7467
type channel struct {
7568
transport Transport
7669
sessionID string
@@ -317,81 +310,18 @@ func (c *channel) SendMessage(ctx context.Context, msg *Message) error {
317310
return c.sendToTransport(ctx, msg, "send message")
318311
}
319312

320-
func (c *channel) ReceiveMessage(ctx context.Context) (*Message, error) {
321-
if err := c.ensureEstablished("receive message"); err != nil {
322-
return nil, err
323-
}
324-
325-
select {
326-
case <-ctx.Done():
327-
return nil, fmt.Errorf("receive message: %w", ctx.Err())
328-
case msg, ok := <-c.inMsgChan:
329-
if !ok {
330-
return nil, errors.New("receive message: channel closed")
331-
}
332-
return msg, nil
333-
}
334-
}
335313
func (c *channel) SendNotification(ctx context.Context, not *Notification) error {
336314
return c.sendToTransport(ctx, not, "send notification")
337315
}
338316

339-
func (c *channel) ReceiveNotification(ctx context.Context) (*Notification, error) {
340-
if err := c.ensureEstablished("receive notification"); err != nil {
341-
return nil, err
342-
}
343-
344-
select {
345-
case <-ctx.Done():
346-
return nil, fmt.Errorf("receive notification: %w", ctx.Err())
347-
case not, ok := <-c.inNotChan:
348-
if !ok {
349-
return nil, errors.New("receive notification: channel closed")
350-
}
351-
return not, nil
352-
}
353-
}
354-
355317
func (c *channel) SendRequestCommand(ctx context.Context, cmd *RequestCommand) error {
356318
return c.sendToTransport(ctx, cmd, "send request command")
357319
}
358320

359-
func (c *channel) ReceiveRequestCommand(ctx context.Context) (*RequestCommand, error) {
360-
if err := c.ensureEstablished("receive request command"); err != nil {
361-
return nil, err
362-
}
363-
364-
select {
365-
case <-ctx.Done():
366-
return nil, fmt.Errorf("receive request command: %w", ctx.Err())
367-
case cmd, ok := <-c.inReqCmdChan:
368-
if !ok {
369-
return nil, errors.New("receive request command: channel closed")
370-
}
371-
return cmd, nil
372-
}
373-
}
374-
375321
func (c *channel) SendResponseCommand(ctx context.Context, cmd *ResponseCommand) error {
376322
return c.sendToTransport(ctx, cmd, "send response command")
377323
}
378324

379-
func (c *channel) ReceiveResponseCommand(ctx context.Context) (*ResponseCommand, error) {
380-
if err := c.ensureEstablished("receive response command"); err != nil {
381-
return nil, err
382-
}
383-
384-
select {
385-
case <-ctx.Done():
386-
return nil, fmt.Errorf("receive response command: %w", ctx.Err())
387-
case cmd, ok := <-c.inRespCmdChan:
388-
if !ok {
389-
return nil, errors.New("receive response command: channel closed")
390-
}
391-
return cmd, nil
392-
}
393-
}
394-
395325
func (c *channel) ProcessCommand(ctx context.Context, reqCmd *RequestCommand) (*ResponseCommand, error) {
396326
return c.processCommand(ctx, c, reqCmd)
397327
}

channel_test.go

Lines changed: 60 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -213,30 +213,14 @@ func TestChannel_ReceiveMessage_WhenEstablished(t *testing.T) {
213213
_ = server.Send(ctx, m)
214214

215215
// Act
216-
actual, err := c.ReceiveMessage(ctx)
217-
218-
// Assert
219-
assert.NoError(t, err)
220-
assert.Equal(t, m, actual)
221-
}
222-
223-
func TestChannel_ReceiveMessage_WhenContextDeadline(t *testing.T) {
224-
// Arrange
225-
defer goleak.VerifyNone(t)
226-
client, _ := newInProcessTransportPair("localhost", 1)
227-
c := newChannel(client, 1)
228-
defer silentClose(c)
229-
c.setState(SessionStateEstablished)
230-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
231-
defer cancel()
232-
233-
// Act
234-
actual, err := c.ReceiveMessage(ctx)
235-
236-
// Assert
237-
assert.Error(t, err)
238-
assert.Equal(t, "receive message: context deadline exceeded", err.Error())
239-
assert.Nil(t, actual)
216+
select {
217+
case <-ctx.Done():
218+
t.Fatal(ctx.Err())
219+
case actual, ok := <-c.MsgChan():
220+
// Assert
221+
assert.True(t, ok)
222+
assert.Equal(t, m, actual)
223+
}
240224
}
241225

242226
func TestChannel_ReceiveMessage_WhenFinishedState(t *testing.T) {
@@ -262,12 +246,16 @@ func receiveMessageWithState(t *testing.T, state SessionState) {
262246
time.Sleep(50 * time.Millisecond)
263247
c.setState(state)
264248
}()
265-
actual, err := c.ReceiveMessage(ctx)
266249

267-
// Assert
268-
assert.Error(t, err)
269-
assert.Equal(t, "receive message: channel closed", err.Error())
270-
assert.Nil(t, actual)
250+
// Act
251+
select {
252+
case <-ctx.Done():
253+
t.Fatal(ctx.Err())
254+
case actual, ok := <-c.MsgChan():
255+
// Assert
256+
assert.False(t, ok)
257+
assert.Nil(t, actual)
258+
}
271259
}
272260

273261
func TestChannel_SendNotification_WhenEstablished(t *testing.T) {
@@ -429,30 +417,14 @@ func TestChannel_ReceiveNotification_WhenEstablished(t *testing.T) {
429417
_ = server.Send(ctx, n)
430418

431419
// Act
432-
actual, err := c.ReceiveNotification(ctx)
433-
434-
// Assert
435-
assert.NoError(t, err)
436-
assert.Equal(t, n, actual)
437-
}
438-
439-
func TestChannel_ReceiveNotification_WhenContextCanceled(t *testing.T) {
440-
// Arrange
441-
defer goleak.VerifyNone(t)
442-
client, _ := newInProcessTransportPair("localhost", 1)
443-
c := newChannel(client, 1)
444-
defer silentClose(c)
445-
c.setState(SessionStateEstablished)
446-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
447-
defer cancel()
448-
449-
// Act
450-
actual, err := c.ReceiveNotification(ctx)
451-
452-
// Assert
453-
assert.Error(t, err)
454-
assert.Equal(t, "receive notification: context deadline exceeded", err.Error())
455-
assert.Nil(t, actual)
420+
select {
421+
case <-ctx.Done():
422+
t.Fatal(ctx.Err())
423+
case actual, ok := <-c.NotChan():
424+
// Assert
425+
assert.True(t, ok)
426+
assert.Equal(t, n, actual)
427+
}
456428
}
457429

458430
func TestChannel_ReceiveNotification_WhenFinishedState(t *testing.T) {
@@ -479,12 +451,16 @@ func receiveNotificationWithState(t *testing.T, state SessionState) {
479451
time.Sleep(50 * time.Millisecond)
480452
c.setState(state)
481453
}()
482-
actual, err := c.ReceiveNotification(ctx)
483454

484-
// Assert
485-
assert.Error(t, err)
486-
assert.Equal(t, "receive notification: channel closed", err.Error())
487-
assert.Nil(t, actual)
455+
// Act
456+
select {
457+
case <-ctx.Done():
458+
t.Fatal(ctx.Err())
459+
case actual, ok := <-c.NotChan():
460+
// Assert
461+
assert.False(t, ok)
462+
assert.Nil(t, actual)
463+
}
488464
}
489465

490466
func TestChannel_SendRequestCommand_WhenEstablished(t *testing.T) {
@@ -648,30 +624,14 @@ func TestChannel_ReceiveCommand_WhenEstablished(t *testing.T) {
648624
_ = server.Send(ctx, cmd)
649625

650626
// Act
651-
actual, err := c.ReceiveRequestCommand(ctx)
652-
653-
// Assert
654-
assert.NoError(t, err)
655-
assert.Equal(t, cmd, actual)
656-
}
657-
658-
func TestChannel_ReceiveCommand_WhenContextCanceled(t *testing.T) {
659-
// Arrange
660-
defer goleak.VerifyNone(t)
661-
client, _ := newInProcessTransportPair("localhost", 1)
662-
c := newChannel(client, 1)
663-
defer silentClose(c)
664-
c.setState(SessionStateEstablished)
665-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
666-
defer cancel()
667-
668-
// Act
669-
actual, err := c.ReceiveRequestCommand(ctx)
670-
671-
// Assert
672-
assert.Error(t, err)
673-
assert.Equal(t, "receive request command: context deadline exceeded", err.Error())
674-
assert.Nil(t, actual)
627+
select {
628+
case <-ctx.Done():
629+
t.Fatal(ctx.Err())
630+
case actual, ok := <-c.ReqCmdChan():
631+
// Assert
632+
assert.True(t, ok)
633+
assert.Equal(t, cmd, actual)
634+
}
675635
}
676636

677637
func TestChannel_ReceiveCommand_WhenFinishedState(t *testing.T) {
@@ -698,12 +658,16 @@ func receiveCommandWithState(t *testing.T, state SessionState) {
698658
time.Sleep(50 * time.Millisecond)
699659
c.setState(state)
700660
}()
701-
actual, err := c.ReceiveRequestCommand(ctx)
702661

703-
// Assert
704-
assert.Error(t, err)
705-
assert.Equal(t, "receive request command: channel closed", err.Error())
706-
assert.Nil(t, actual)
662+
// Act
663+
select {
664+
case <-ctx.Done():
665+
t.Fatal(ctx.Err())
666+
case actual, ok := <-c.ReqCmdChan():
667+
// Assert
668+
assert.False(t, ok)
669+
assert.Nil(t, actual)
670+
}
707671
}
708672

709673
func TestChannel_ProcessCommand(t *testing.T) {
@@ -787,7 +751,13 @@ func TestChannel_ProcessCommand_ResponseWithAnotherId(t *testing.T) {
787751
assert.Nil(t, actual)
788752
ctx, cancel = context.WithTimeout(context.Background(), 250*time.Millisecond)
789753
defer cancel()
790-
actualRespCmd, err := c.ReceiveResponseCommand(ctx)
791-
assert.NoError(t, err)
792-
assert.Equal(t, respCmd, actualRespCmd)
754+
755+
// Act
756+
select {
757+
case <-ctx.Done():
758+
t.Fatal(ctx.Err())
759+
case actualRespCmd, ok := <-c.RespCmdChan():
760+
assert.True(t, ok)
761+
assert.Equal(t, respCmd, actualRespCmd)
762+
}
793763
}

0 commit comments

Comments
 (0)