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

ctrl.Finish() is not called in 1.14+ #447

Closed
@sarpdoruk

Description

@sarpdoruk

It is stated that if go version is 1.14+ and *t.Testing is passed into the controller it is not needed to call ctrl.Finish().

Actual behavior
Although I'm using 1.14.3, ctrl.Finish() is not called when, considering Times(x), x is greater than the actual number calls and results in a successful test. This is not the case when x is smaller than the number of calls made then it throws the following error as expected;
expected call at ..._test.go:XX has already been called the max number of times

Expected behavior
When Times(x) is used to track number of calls and x is not equal to the number of calls, tests should fail.

To Reproduce

1- Use GO 1.14.3
2- Call a method 3 times in your actual code
3- Do not call ctrl.Finish() in your test
4- Test the following cases;

  • If you use Times(1) in your test, it fails. (As expected)
  • If you use Times(3) in your test, it passes. (As expected)
  • If you use Times(172863) in your test, it passes. (Not expected)

Additional Information

  • gomock mode (reflect or source): source
  • gomock version or git ref: 1.4.3
  • golang version: 1.14.3

Example
main.go

package main

import (
	"fmt"
	"io"
	"time"
)

type Sleeper interface {
	Sleep(d time.Duration)
}

type DefaultSleeper struct{}

func (s *DefaultSleeper) Sleep(d time.Duration) {
	time.Sleep(d)
}

func Countdown(w io.Writer, s Sleeper) {
	for i := 3; i > 0; i-- {
		fmt.Fprintf(w, "%d\n", i)
		s.Sleep(time.Second)
	}

	fmt.Fprint(w, "Go!")
}

func main() {
	Countdown(os.Stdout, &DefaultSleeper{})
}

main_test.go

package main

import (
	"bytes"
	"github.com/golang/mock/gomock"
	"testing"
	"time"
)

func TestCountdown(t *testing.T) {
	buffer := &bytes.Buffer{}
	ctrl := gomock.NewController(t)
	// defer ctrl.Finish()
	s := NewMockSleeper(ctrl)

	s.
		EXPECT().
		Sleep(gomock.Eq(time.Second)).
		Times(172863)

	Countdown(buffer, s)

	got := buffer.String()
	want := `3
2
1
Go!`

	if got != want {
		t.Errorf("got %q, want %q", got, want)
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions