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

causalityDuration Hides code execution in duration method #233

Closed
v-zubko opened this issue Feb 26, 2020 · 4 comments
Closed

causalityDuration Hides code execution in duration method #233

v-zubko opened this issue Feb 26, 2020 · 4 comments

Comments

@v-zubko
Copy link
Contributor

v-zubko commented Feb 26, 2020

func (d *causalityDuration) duration() time.Duration {

It is not expected that duration method will actually do such sophisticated thing. I expect it to return duration value only.

@teivah
Copy link
Member

teivah commented Feb 26, 2020

I disagree, it is expected. Actually, that's the purpose of it. Let's see why with a concrete example.

There's this BufferWithTime operator that batches the items at a given frequency. How would we implement unit tests for that? Most likely using a couple of time.Sleep(), right?

Now, let's see how the nominal test is implemented:

func Test_Observable_BufferWithTime(t *testing.T) {
	ctx, obs, d := timeCausality(1, 2, 3, tick, tick, 4, tick, 5, 6, 7, tick)
	obs = obs.BufferWithTime(d, WithContext(ctx))
	Assert(context.Background(), t, obs, HasItems(
		[]interface{}{1, 2, 3},
		[]interface{}{4},
		[]interface{}{5, 6, 7},
	))
}

Using timeCausality() that returns an Observable, we can configure whenever the timer is going to be triggered. This creates a deterministic unit test without any single time.Sleep().

If you need a simple mock, you can use mockDuration.

@teivah teivah mentioned this issue Feb 26, 2020
@v-zubko
Copy link
Contributor Author

v-zubko commented Feb 26, 2020

I understand why it is there and how it works. My point is that it is not obvious and may confuse others.
As to example with test it could be like this:

buf := <-Interval(WithDuration(time.Second), WithContext(ctx)).BufferWithTime(3 * time.Second, WithContext(ctx)).Observe()
assert.Len(t, buf, 3)

@v-zubko
Copy link
Contributor Author

v-zubko commented Feb 26, 2020

Another thing is that introducing artificial pauses while sending data will hide things like this #234

@teivah
Copy link
Member

teivah commented Feb 26, 2020

Let's keep only #234

@teivah teivah closed this as completed Feb 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants