Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give assert.CollectT a Logf Method to allow use as mock.TestingT #1414

Open
brackendawson opened this issue Jul 11, 2023 · 3 comments
Open
Labels
enhancement pkg-assert Change related to package testify/assert pkg-mock Any issues related to Mock

Comments

@brackendawson
Copy link
Collaborator

From #1030: It would be nice if assert.CollectT implimented mock.TestingT so that this can work:

package kata

import (
	"fmt"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
)

type myMock struct {
	mock.Mock
}

func (m *myMock) Callback(n int) error {
	return m.Called(n).Error(0)
}

func TestIfy(t *testing.T) {
	m := myMock{}
	m.Test(t)
	for i := 0; i < 10; i++ {
		m.On("Callback", i).Return(nil).Once()
	}

	go func() {
		for i := 0; i < 10; i++ {
			if err := m.Callback(i); err != nil {
				panic(fmt.Sprint("callback failed: ", err))
			}
		}
	}()

	assert.EventuallyWithT(t, func(t *assert.CollectT) {
		m.AssertExpectations(t)
	}, time.Second, time.Millisecond)
}
@dolmen
Copy link
Collaborator

dolmen commented Jul 11, 2023

I think that the collection process must be seriously rewritten because of #1418.

@dolmen dolmen added pkg-mock Any issues related to Mock pkg-assert Change related to package testify/assert enhancement labels Jul 11, 2023
@dolmen
Copy link
Collaborator

dolmen commented Jul 31, 2023

I would like to get rid of assert.CollectT and just pass an opaque assert.TestingT to the condition function.

@dolmen
Copy link
Collaborator

dolmen commented Jul 31, 2023

The issue is more generally about mock.TestingT being a superset of assert.TestingT which means that a function being given an assert.TestingT can't pass it to mock functions.

andrzej-stencel added a commit to andrzej-stencel/elastic-agent that referenced this issue Sep 3, 2024
The flakiness is a result of a 100 miliseconds not always being enough time to  dispatch the action in the test environment.

The correct way to fix this issue is to run the assertions in an `assert.Eventually` with a longer timeout. Unfortunately, a deficiency in the `testify` testing framework makes it impossible to put the `AssertExpectations` methods in an `assert.Eventually` or `assert.EventuallyWithT` function: stretchr/testify#1414.

The workaround is to increase the context timeout for the `gateway actions passed` test case. I'm increasing it from 100ms to 200ms.
andrzej-stencel added a commit to elastic/elastic-agent that referenced this issue Sep 5, 2024
* refctor: make context timeout adjustable per test case

A short context timeout makes some tests flaky, but making it longer across the board would unnecessarily make test runs longer. Let's make it customizable per test case.

* Fix flaky test `Test_runDispatcher/gateway_actions_passed`

The flakiness is a result of a 100 miliseconds not always being enough time to  dispatch the action in the test environment.

The correct way to fix this issue is to run the assertions in an `assert.Eventually` with a longer timeout. Unfortunately, a deficiency in the `testify` testing framework makes it impossible to put the `AssertExpectations` methods in an `assert.Eventually` or `assert.EventuallyWithT` function: stretchr/testify#1414.

The workaround is to increase the context timeout for the `gateway actions passed` test case. I'm increasing it from 100ms to 200ms.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement pkg-assert Change related to package testify/assert pkg-mock Any issues related to Mock
Projects
None yet
Development

No branches or pull requests

2 participants