Skip to content

cassandra, scylladb and dgraph test helpers call ctrl.Finish() early, so missing mock calls are never reported #4315

Closed

Description

@NitinKumar004

Describe the bug

The shared test helpers in three datasource packages create a gomock controller and defer ctrl.Finish() inside the helper itself:

  • pkg/gofr/datasource/cassandra/cassandra_test.go:33 (initTest)
  • pkg/gofr/datasource/scylladb/scylladb_test.go:32 (initTest)
  • pkg/gofr/datasource/dgraph/dgraph_test.go:26 (setupDB)
func initTest(t *testing.T) (*Client, *mockDependencies) {
    t.Helper()

    ctrl := gomock.NewController(t)
    defer ctrl.Finish()
    ...
    return client, &mockDependencies{...}
}

The deferred Finish runs when the helper returns, before any test has registered its expectations. gomock.NewController(t) also registers a Finish through t.Cleanup, but in go.uber.org/mock v0.6.0 that one short-circuits because the controller is already marked finished (gomock/controller.go:268-273). So "missing call(s)" is never reported for any test that uses these helpers.

In practice every .Times(1) in those packages only catches a wrong call, never a call that didn't happen. During the #4270 review, replacing cassandra's HealthCheck body so it skipped the probe query entirely still passed the test until require.True(t, ctrl.Satisfied()) was added.

Expected behavior

Drop the defer ctrl.Finish() from the three helpers and let the t.Cleanup registered by gomock.NewController(t) do the check at the end of the test. Some existing tests will probably start failing on expectations that were never met, and those should be fixed or removed rather than loosened.

Additional context

Found by @aryanmehrotra while reviewing #4270 and #4274.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions