You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

47 lines
1.0 KiB

  1. package concurrent
  2. import (
  3. "testing"
  4. "context"
  5. "github.com/golang/mock/gomock"
  6. mock "github.com/golang/mock/sample/concurrent/mock"
  7. )
  8. func call(ctx context.Context, m Math) (int, error) {
  9. result := make(chan int)
  10. go func() {
  11. result <- m.Sum(1, 2)
  12. close(result)
  13. }()
  14. select {
  15. case r := <-result:
  16. return r, nil
  17. case <-ctx.Done():
  18. return 0, ctx.Err()
  19. }
  20. }
  21. // testConcurrentFails is expected to fail (and is disabled). It
  22. // demonstrates how to use gomock.WithContext to interrupt the test
  23. // from a different goroutine.
  24. func testConcurrentFails(t *testing.T) {
  25. ctrl, ctx := gomock.WithContext(context.Background(), t)
  26. defer ctrl.Finish()
  27. m := mock.NewMockMath(ctrl)
  28. if _, err := call(ctx, m); err != nil {
  29. t.Error("call failed:", err)
  30. }
  31. }
  32. func TestConcurrentWorks(t *testing.T) {
  33. ctrl, ctx := gomock.WithContext(context.Background(), t)
  34. defer ctrl.Finish()
  35. m := mock.NewMockMath(ctrl)
  36. m.EXPECT().Sum(1, 2).Return(3)
  37. if _, err := call(ctx, m); err != nil {
  38. t.Error("call failed:", err)
  39. }
  40. }