Skip to content

Commit

Permalink
Merge pull request #9 from paultyng/pt/context
Browse files Browse the repository at this point in the history
Add context.Context to NewWriter
  • Loading branch information
Chris Raborg authored Oct 21, 2017
2 parents 3f27e70 + a23c9bd commit 545963e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 26 deletions.
6 changes: 3 additions & 3 deletions mem/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Example_primitives() {

for _, row := range lines {
for _, col := range row {
w := t2.NewWriter()
w := t2.NewWriter(ctx)
w.Write([]byte(col))
w.Close()
}
Expand All @@ -45,7 +45,7 @@ func Example_primitives() {
return err
}

w := t3.NewWriter()
w := t3.NewWriter(ctx)
w.Attributes().Set("Length", fmt.Sprintf("%d", len(body)))
w.Attributes().Set("StartsWith", string(body[0:1]))

Expand All @@ -67,7 +67,7 @@ func Example_primitives() {
}

for _, m := range messages {
w := t1.NewWriter()
w := t1.NewWriter(context.Background())
w.Write(m)
w.Close()
}
Expand Down
15 changes: 0 additions & 15 deletions mem/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,6 @@ func (r *ConcurrentReceiver) Receive(ctx context.Context, m *msg.Message) error
return nil
}

// PanicReceiver panics upon consumption of a message.
// It is safe to utilize by concurrent goroutines.
type PanicReceiver struct {
t *testing.T
}

func (r *PanicReceiver) Receive(ctx context.Context, m *msg.Message) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
panic("AHHH")
}
}

// RetryReceiver returns an error upon consumption of a Message. Once it
// has been called a certain number of times, it writes to an channel and
// returns nil.
Expand Down
3 changes: 2 additions & 1 deletion mem/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mem

import (
"bytes"
"context"
"sync"

"github.com/zerofox-oss/go-msg"
Expand All @@ -17,7 +18,7 @@ var _ msg.Topic = &Topic{}

// NewWriter returns a MessageWriter.
// The MessageWriter may be used to write messages to a channel.
func (t *Topic) NewWriter() msg.MessageWriter {
func (t *Topic) NewWriter(context.Context) msg.MessageWriter {
return &MessageWriter{
c: t.C,

Expand Down
7 changes: 4 additions & 3 deletions mem/topic_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mem_test

import (
"context"
"testing"

"github.com/zerofox-oss/go-msg"
Expand All @@ -10,7 +11,7 @@ import (
func TestMessageWriter_Attributes(t *testing.T) {
testTopic := &mem.Topic{}

w := testTopic.NewWriter()
w := testTopic.NewWriter(context.Background())
attrs := w.Attributes()
attrs.Set("test", "value")

Expand All @@ -26,7 +27,7 @@ func TestMessageWriter_WriteAndClose(t *testing.T) {
}

go func() {
w := testTopic.NewWriter()
w := testTopic.NewWriter(context.Background())
w.Write([]byte("Don't "))
w.Write([]byte("call me "))
w.Write([]byte("junior!"))
Expand All @@ -53,7 +54,7 @@ func TestMesageWriter_SingleUse(t *testing.T) {
C: channel,
}

w := testTopic.NewWriter()
w := testTopic.NewWriter(context.Background())

text := [][]byte{
[]byte("I have a bad feeling about this..."),
Expand Down
11 changes: 7 additions & 4 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,17 @@ type MessageWriter interface {
// Multiple goroutines may invoke method on a Topic simultaneously.
type Topic interface {
// NewWriter returns a new MessageWriter
NewWriter() MessageWriter
NewWriter(context.Context) MessageWriter
}

// The TopicFunc is an adapter to allow the use of ordinary functions
// as a Topic. TopicFunc(f) is a Topic that calls f.
type TopicFunc func() MessageWriter
type TopicFunc func(context.Context) MessageWriter

// Ensure TopicFunc implements Topic
var _ Topic = TopicFunc(nil)

// NewWriter calls f(ctx,m)
func (t TopicFunc) NewWriter() MessageWriter {
return t()
func (t TopicFunc) NewWriter(ctx context.Context) MessageWriter {
return t(ctx)
}

0 comments on commit 545963e

Please sign in to comment.