Skip to content

Add SyncSuite and split existing suite code into reusable parts.#1837

Open
Nocccer wants to merge 4 commits intostretchr:masterfrom
Nocccer:feature/synctest-suite
Open

Add SyncSuite and split existing suite code into reusable parts.#1837
Nocccer wants to merge 4 commits intostretchr:masterfrom
Nocccer:feature/synctest-suite

Conversation

@Nocccer
Copy link

@Nocccer Nocccer commented Dec 7, 2025

Summary

This PR adds a new suite SyncSuite to run each suite test inside a synctest bubble.

Hit me up if you miss some information or have some ideas to improve it.

Changes

  • Splitted already existing suite code into small reusable functions
  • Add new SyncSuite suite with own RunSync function
  • Add method SetTime to set a fixed time in the bubble. This is just a helper for time.Until(ts) which is a trick to set a specific timestamp.
  • Add method Wait as a wrapper for synctest.Wait
  • Copied and adapt already existing suite tests to test syncsuite
  • Added some small tests to test if suite tests are run inside a bubble

Motivation

The current suite is not usable for synctest if you define tickers, channels, etc inside SetupTest.
I want to have a suite where each test is run inside a synctest bubble and handles SetupTest and TeardownTest correctly (run after synctest.Test).

The current workaround would be to call SetupTest again after synctest.Test was called, which is not the intended way.

Here is a codesnippet of the problem in the current suite:

type TestSuite struct {
  suite.Suite
  ticker *time.Ticker
}

func (s *TestSuite) SetupTest() {
  s.ticker = time.NewTicker(time.Second)
}

func (s *TestSuite) TestTicker() {
  synctest.Test(s.T(), func(t *testing.T) {
    // this will still run 1s because the ticker is created outside the bubble
    <-s.ticker.C
  })
}

Here is the current workaround to make it work:

type TestSuite struct {
  suite.Suite
  ticker *time.Ticker
}

func (s *TestSuite) SetupTest() {
  s.ticker = time.NewTicker(time.Second)
}

func (s *TestSuite) TestTicker() {
  synctest.Test(s.T(), func(t *testing.T) {
    // call SetupTest again to create the ticker inside the bubble
    s.SetupTest()

    // this will not take 1s
    <-s.ticker.C
  })
}

Not Supported

  • SetupSuite and TearDownSuite is not supported because each bubble should have a fresh state. SetupSuite could be supported if the people using the new suite never setup time related variables inside it. Because we cannot ensure this, i removed it for now.
  • SetupSubTest and TearDownSubTest is not supported because you can't run a bubble inside a bubble (run synctest.Test inside synctest.Test)

Related issues

#1836
#1834

@Nocccer Nocccer marked this pull request as ready for review January 18, 2026 14:07
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

Successfully merging this pull request may close these issues.

1 participant