Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gochan): implement SendCloser on SendReceiver protocol #768

Merged
merged 1 commit into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion v2/protocol/gochan/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package gochan

import (
"context"

"github.com/cloudevents/sdk-go/v2/binding"
"github.com/cloudevents/sdk-go/v2/protocol"
)
Expand All @@ -18,7 +19,7 @@ const (
// SendReceiver is a reference implementation for using the CloudEvents binding
// integration.
type SendReceiver struct {
sender protocol.Sender
sender protocol.SendCloser
receiver protocol.Receiver
}

Expand All @@ -38,3 +39,7 @@ func (sr *SendReceiver) Send(ctx context.Context, in binding.Message, transforme
func (sr *SendReceiver) Receive(ctx context.Context) (binding.Message, error) {
return sr.receiver.Receive(ctx)
}

func (sr *SendReceiver) Close(ctx context.Context) error {
return sr.sender.Close(ctx)
}
73 changes: 70 additions & 3 deletions v2/protocol/gochan/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ package gochan

import (
"context"
"io"
"testing"
"time"

"github.com/cloudevents/sdk-go/v2/binding"
"github.com/cloudevents/sdk-go/v2/event"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"io"
"testing"
"time"
)

func TestNew(t *testing.T) {
Expand Down Expand Up @@ -128,6 +129,72 @@ func TestSendReceive(t *testing.T) {
}
}

func TestSendCloser(t *testing.T) {
testCases := map[string]struct {
numSend int
numReceivePreClose int
numClose int // defaults to 1
wantErr bool
}{
"closes none pending": {
numSend: 1,
numReceivePreClose: 1,
},
"closes still delivers pending": {
numSend: 2,
numReceivePreClose: 1,
},
"errors on double close": {
numClose: 2,
wantErr: true,
},
}
for n, tc := range testCases {
for _, p := range protocols(t) {
t.Run(n, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
for i := 0; i < tc.numSend; i++ {
e := event.New()
if err := p.Send(ctx, binding.ToMessage(&e)); err != nil {
t.Fatalf("failed to send to protocol: %v", err)
}
}

for i := 0; i < tc.numReceivePreClose; i++ {
_, err := p.Receive(ctx)
if err != nil {
t.Fatalf("failed to receive from protocol: %v", err)
}
}

if tc.numClose == 0 {
tc.numClose = 1
}

var err error
for i := 0; i < tc.numClose; i++ {
err = p.Close(ctx)
}
if tc.wantErr != (err != nil) {
t.Fatalf("failed to close channel, wantErr = %v, got = %v", tc.wantErr, err)
}

for i := 0; i < tc.numSend-tc.numReceivePreClose; i++ {
_, err := p.Receive(ctx)
if err != nil {
t.Fatalf("failed to receive from protocol: %v", err)
}
}

if _, err = p.Receive(ctx); err != io.EOF {
t.Fatalf("expected protocol to be closed but got err = %v", err)
}
})
}
}
}

func ReceiveTest(t *testing.T, p *SendReceiver, ctx context.Context, want binding.Message, wantErr string) {
if ctx != nil {
var done context.CancelFunc
Expand Down