Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

gomock.WithContext: controller with a context #111

Merged
merged 2 commits into from
Oct 10, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions gomock/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
package gomock

import (
"context"
"fmt"
"reflect"
"runtime"
Expand Down Expand Up @@ -85,6 +86,24 @@ func NewController(t TestReporter) *Controller {
}
}

type cancelReporter struct {
t TestReporter
cancel func()
}

func (r *cancelReporter) Errorf(format string, args ...interface{}) { r.t.Errorf(format, args...) }
func (r *cancelReporter) Fatalf(format string, args ...interface{}) {
defer r.cancel()
r.t.Fatalf(format, args...)
}

// WithContext returns a new Controller and a Context, which is cancelled on any
// fatal failure.
func WithContext(ctx context.Context, t TestReporter) (*Controller, context.Context) {
ctx, cancel := context.WithCancel(ctx)
return NewController(&cancelReporter{t, cancel}), ctx
}

func (ctrl *Controller) RecordCall(receiver interface{}, method string, args ...interface{}) *Call {
recv := reflect.ValueOf(receiver)
for i := 0; i < recv.Type().NumMethod(); i++ {
Expand Down
8 changes: 8 additions & 0 deletions sample/concurrent/concurrent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:generate mockgen -destination mock/concurrent_mock.go github.com/golang/mock/sample/concurrent Math

// Package concurrent demonstrates how to use gomock with goroutines.
package concurrent

type Math interface {
Sum(a, b int) int
}
45 changes: 45 additions & 0 deletions sample/concurrent/concurrent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package concurrent

import (
"context"
"testing"

"github.com/golang/mock/gomock"
mock "github.com/golang/mock/sample/concurrent/mock"
)

func call(ctx context.Context, m Math) (int, error) {
result := make(chan int)
go func() {
result <- m.Sum(1, 2)
close(result)
}()
select {
case r := <-result:
return r, nil
case <-ctx.Done():
return 0, ctx.Err()
}
}

// testConcurrentFails is expected to fail (and is disabled). It
// demonstrates how to use gomock.WithContext to interrupt the test
// from a different goroutine.
func testConcurrentFails(t *testing.T) {
ctrl, ctx := gomock.WithContext(context.Background(), t)
defer ctrl.Finish()
m := mock.NewMockMath(ctrl)
if _, err := call(ctx, m); err != nil {
t.Error("call failed:", err)
}
}

func TestConcurrentWorks(t *testing.T) {
ctrl, ctx := gomock.WithContext(context.Background(), t)
defer ctrl.Finish()
m := mock.NewMockMath(ctrl)
m.EXPECT().Sum(1, 2).Return(3)
if _, err := call(ctx, m); err != nil {
t.Error("call failed:", err)
}
}
45 changes: 45 additions & 0 deletions sample/concurrent/mock/concurrent_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.