-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: testing/synctest: new package for testing concurrent code #67434
Comments
I really like how simple this API is.
How does time work when goroutines aren't idle? Does it stand still, or does it advance at the usual rate? If it stands still, it seems like that could break software that assumes time will advance during computation (that maybe that's rare in practice). If it advances at the usual rate, it seems like that reintroduces a source of flakiness. E.g., in your example, the 1 second sleep will advance time by 1 second, but then on a slow system the checking thread may still not execute for a long time. What are the bounds of the fake time implementation? Presumably if you're making direct system calls that interact with times or durations, we're not going to do anything about that. Are we going to make any attempt at faking time in the file system?
What if a goroutine is blocked on a channel that goes outside the group? This came to mind in the context of whether this could be used to coordinate a multi-process client/server test, though I think it would also come up if there's any sort of interaction with a background worker goroutine or pool.
What happens if multiple goroutines in a group call Wait? I think the options are to panic or to consider all of them idle, in which case they would all wake up when every other goroutine in the group is idle. What happens if you have nested groups, say group A contains group B, and a goroutine in B is blocked in Wait, and then a goroutine in A calls Wait? I think your options are to panic (though that feels wrong), wake up both if all of the goroutines in group A are idle, or wake up just B if all of the goroutines in B are idle (but this block waking up A until nothing is calling Wait in group B). |
Time stands still, except when all goroutines in a group are idle. (Same as the playground behaves, I believe.) This would break software that assumes time will advance. You'd need to use something else to test that case.
The time package: Faking time in the filesystem seems complicated and highly specialized, so I don't think we should try. Code which cares about file timestamps will need to use a test
As proposed, this would count as an idle goroutine. If you fail to isolate the system under test this will probably cause problems, so don't do that.
As proposed, none of them ever wake up and your test times out, or possibly panics if we can detect that all goroutines are blocked in that case. Having them all wake at the same time would also be reasonable.
Oh, I didn't think of that. Nested groups are too complicated, |
This is a very interesting proposal! I feel worried that the Assuming that's a valid concern (if it isn't then I'll retract this entire comment!), I could imagine mitigating it in two different ways:
(I apologize in advance if I misunderstood any part of the proposal or if I am missing something existing that's already similarly convenient to |
The fact that I think using idle-wait synchronization outside of tests is always going to be a mistake. It's fragile and fiddly, and you're better served by explicit synchronization. (This prompts the question: Isn't this fragile and fiddly inside tests as well? It is, but using a fake clock removes much of the sources of fragility, and tests often have requirements that make the fiddliness a more worthwhile tradeoff. In the expiring cache example, for example, non-test code will never need to guarantee that a cache entry expires precisely at the nanosecond defined.) So while perhaps we could offer a standalone synchroniziation primitive outside of As for passing a |
Interesting proposal. I like that it allows for waiting for a group of goroutines, as opposed to all goroutines in my proposal (#65336), though I do have some concerns:
|
One of the goals of this proposal is to minimize the amount of unnatural code required to make a system testable. Mock time implementations require replacing calls to idiomatic time package functions with a testable interface. Putting fake time in the standard library would let us just write the idiomatic code without compromising testability. For timeouts, the Also, it would be pointless for |
I wanted to evaluate practical usage of the proposed API. I wrote a version of Run and Wait based on parsing the output of runtime.Stack. Wait calls runtime.Gosched in a loop until all goroutines in the current group are idle. I also wrote a fake time implementation. Combined, these form a reasonable facsimile of the proposed synctest package, with some limitations: The code under test needs to be instrumented to call the fake time functions, and to call a marking function after creating new goroutines. Also, you need to call a synctest.Sleep function in tests to advance the fake clock. I then added this instrumentation to net/http. The synctest package does not work with real network connections, so I added an in-memory net.Conn implementation to the net/http tests. I also added an additional helper to net/http's tests, which simplifies some of the experimentation below: var errStillRunning = errors.New("async op still running")
// asyncResult is the result of an asynchronous operation.
type asyncResult[T any] struct {}
// runAsync runs f in a new goroutine,
// and returns an asyncResult which is populated with the result of f when it finishes.
// runAsync calls synctest.Wait after running f.
func runAsync[T any](f func() (T, error)) *asyncResult[T]
// done reports whether the asynchronous operation has finished.
func (r *asyncResult[T]) done() bool
// result returns the result of the asynchronous operation.
// It returns errStillRunning if the operation is still running.
func (r *asyncResult[T]) result() (T, error) One of the longest-running tests in the net/http package is TestServerShutdownStateNew (https://go.googlesource.com/go/+/refs/tags/go1.22.3/src/net/http/serve_test.go#5611). This test creates a server, opens a connection to it, and calls Server.Shutdown. It asserts that the server, which is expected to wait 5 seconds for the idle connection to close, shuts down in no less than 2.5 seconds and no more than 7.5 seconds. This test generally takes about 5-6 seconds to run in both HTTP/1 and HTTP/2 modes. The portion of this test which performs the shutdown is: shutdownRes := make(chan error, 1)
go func() {
shutdownRes <- ts.Config.Shutdown(context.Background())
}()
readRes := make(chan error, 1)
go func() {
_, err := c.Read([]byte{0})
readRes <- err
}()
// TODO(#59037): This timeout is hard-coded in closeIdleConnections.
// It is undocumented, and some users may find it surprising.
// Either document it, or switch to a less surprising behavior.
const expectTimeout = 5 * time.Second
t0 := time.Now()
select {
case got := <-shutdownRes:
d := time.Since(t0)
if got != nil {
t.Fatalf("shutdown error after %v: %v", d, err)
}
if d < expectTimeout/2 {
t.Errorf("shutdown too soon after %v", d)
}
case <-time.After(expectTimeout * 3 / 2):
t.Fatalf("timeout waiting for shutdown")
}
// Wait for c.Read to unblock; should be already done at this point,
// or within a few milliseconds.
if err := <-readRes; err == nil {
t.Error("expected error from Read")
} I wrapped the test in a synctest.Run call and changed it to use the in-memory connection. I then rewrote this section of the test: shutdownRes := runAsync(func() (struct{}, error) {
return struct{}{}, ts.Config.Shutdown(context.Background())
})
readRes := runAsync(func() (int, error) {
return c.Read([]byte{0})
})
// TODO(#59037): This timeout is hard-coded in closeIdleConnections.
// It is undocumented, and some users may find it surprising.
// Either document it, or switch to a less surprising behavior.
const expectTimeout = 5 * time.Second
synctest.Sleep(expectTimeout - 1)
if shutdownRes.done() {
t.Fatal("shutdown too soon")
}
synctest.Sleep(2 * time.Second)
if _, err := shutdownRes.result(); err != nil {
t.Fatalf("Shutdown() = %v, want complete", err)
}
if n, err := readRes.result(); err == nil || err == errStillRunning {
t.Fatalf("Read() = %v, %v; want error", n, err)
} The test exercises the same behavior it did before, but it now runs instantaneously. (0.01 seconds on my laptop.) I made an interesting discovery after converting the test: The server does not actually shut down in 5 seconds. In the initial version of this test, I checked for shutdown exactly 5 seconds after calling Shutdown. The test failed, reporting that the Shutdown call had not completed. Examining the Shutdown function revealed that the server polls for closed connections during shutdown, with a maximum poll interval of 500ms, and therefore shutdown can be delayed slightly past the point where connections have shut down. I changed the test to check for shutdown after 6 seconds. But once again, the test failed. Further investigation revealed this code (https://go.googlesource.com/go/+/refs/tags/go1.22.3/src/net/http/server.go#3041): st, unixSec := c.getState()
// Issue 22682: treat StateNew connections as if
// they're idle if we haven't read the first request's
// header in over 5 seconds.
if st == StateNew && unixSec < time.Now().Unix()-5 {
st = StateIdle
} The comment states that new connections are considered idle for 5 seconds, but thanks to the low granularity of Unix timestamps the test can consider one idle for as little as 4 or as much as 6 seconds. Combined with the 500ms poll interval (and ignoring any added scheduler delay), Shutdown may take up to 6.5 seconds to complete, not 5. Using a fake clock rather than a real one not only speeds up this test dramatically, but it also allows us to more precisely test the behavior of the system under test. Another slow test is TestTransportExpect100Continue (https://go.googlesource.com/go/+/refs/tags/go1.22.3/src/net/http/transport_test.go#1188). This test sends an HTTP request containing an "Expect: 100-continue" header, which indicates that the client is waiting for the server to indicate that it wants the request body before it sends it. In one variation, the server does not send a response; after a 2 second timeout, the client gives up waiting and sends the request. This test takes 2 seconds to execute, thanks to this timeout. In addition, the test does not validate the timing of the client sending the request body; in particular, tests pass even if the client waits The portion of the test which sends the request is: resp, err := c.Do(req) I changed this to: rt := runAsync(func() (*Response, error) {
return c.Do(req)
})
if v.timeout {
synctest.Sleep(expectContinueTimeout-1)
if rt.done() {
t.Fatalf("RoundTrip finished too soon")
}
synctest.Sleep(1)
}
resp, err := rt.result()
if err != nil {
t.Fatal(err)
} This test now executes instantaneously. It also verifies that the client does or does not wait for the ExpectContinueTimeout as expected. I made one discovery while converting this test. The synctest.Run function blocks until all goroutines in the group have exited. (In the proposed synctest package, Run will panic if all goroutines become blocked (deadlock), but I have not implemented that feature in the test version of the package.) The test was hanging in Run, due to leaking a goroutine. I tracked this down to a missing net.Conn.Close call, which was leaving an HTTP client reading indefinitely from an idle and abandoned server connection. In this case, Run's behavior caused me some confusion, but ultimately led to the discovery of a real (if fairly minor) bug in the test. (I'd probably have experienced less confusion, but I initially assumed this was a bug in the implementation of Run.) At one point during this exercise, I accidentally called testing.T.Run from within a synctest.Run group. This results in, at the very best, quite confusing behavior. I think we would want to make it possible to detect when running within a group, and have testing.T.Run panic in this case. My experimental implementation of the synctest package includes a synctest.Sleep function by necessity: It was much easier to implement with an explicit call to advance the fake clock. However, I found in writing these tests that I often want to sleep and then wait for any timers to finish executing before continuing. I think, therefore, that we should have one additional convenience function: package synctest
// Sleep pauses the current goroutine for the duration d,
// and then blocks until every goroutine in the current group is idle.
// It is identical to calling time.Sleep(d) followed by Wait.
//
// The caller of Sleep must be in a goroutine created by Run,
// or a goroutine transitively started by Run.
// If it is not, Sleep panics.
func Sleep(d time.Duration) {
time.Sleep(d)
Wait()
} The net/http package was not designed to support testing with a fake clock. This has served as an obstacle to improving the state of the package's tests, many of which are slow, flaky, or both. Converting net/http to be testable with my experimental version of synctest required a small number of minor changes. A runtime-supported synctest would have required no changes at all to net/http itself. Converting net/http tests to use synctest required adding an in-memory net.Conn. (I didn't attempt to use net.Pipe, because its fully-synchronous behavior tends to cause problems in tests.) Aside from this, the changes required were very small. My experiment is in https://go.dev/cl/587657. |
This proposal has been added to the active column of the proposals project |
Commenting here due to @rsc's request: Relative to my proposal #65336, I have the following concerns:
|
Regarding overriding the The In contrast, we can test code which calls Time is fundamentally different in that there is no way to use real time in a test without making the test flaky and slow. Time is also different from an Since we can't use real time in tests, we can insert a testable wrapper around the In addition, if we define a standard testable wrapper around the clock, we are essentially declaring that all public packages which deal with time should provide a way to plumb in a clock. (Some packages do this already, of course; crypto/tls.Config.Time is an example in That's an option, of course. But it would be a very large change to the Go ecosystem as a whole. |
The pprof.SetGoroutineLabels disagrees.
It doesn't try to hide it, more like tries to restrict people from relying on numbers.
If I understood proposal correctly, it will wait for any goroutine (and recursively) that was started using |
Yes, if you call |
Given that there's more precedent for goroutine identity than I had previously thought, and seeing how However, I'm still a little ambivalent about goroutine groups affecting That being said, I agree that plumbing a time/clock interface through existing code is indeed tedious, and having |
Thanks for doing the experiment. I find the results pretty compelling.
I don't quite understand this function. Given the fake time implementation, if you sleep even a nanosecond past timer expiry, aren't you already guaranteed that those timers will have run because the fake time won't advance to your sleep deadline until everything is blocked again?
Partly I was wondering about nested groups because I've been scheming other things that the concept of a goroutine group could be used for. Though it's true that, even if we have groups for other purposes, it may make sense to say that synctest groups cannot be nested, even if in general groups can be nested. |
You're right that sleeping past the deadline of a timer is sufficient. The It's fairly natural to sleep to the exact instant of a timer, however. If a cache entry expires in some amount of time, it's easy to sleep for that exact amount of time, possibly using the same constant that the cache timeout was initialized with, rather than adding a nanosecond. Adding nanoseconds also adds a small but real amount of confusion to a test in various small ways: The time of logged events drifts off the integer second, rate calculations don't come out as cleanly, and so on. Plus, if you forget to add the necessary adjustment or otherwise accidentally sleep directly onto the instant of a timer's expiry, you get a race condition. Cleaner, I think, for the test code to always resynchronize after poking the system under test. This doesn't have to be a function in the synctest package, of course;
I'm very intrigued! I've just about convinced myself that there's a useful general purpose synchronization API hiding in here, but I'm not sure what it is or what it's useful for. |
For what it's worth, I think it's a good thing that virtual time is included in this, because it makes sure that this package isn't used in production settings. It makes it only suitable for tests (and very suitable). |
It sounds like the API is still:
Damien suggested adding also:
The difference between time.Sleep and synctest.Sleep seems subtle enough that it seems like you should have to spell out the Wait at the call sites where you need it. The only time you really need Wait is if you know someone else is waking up at that very moment. But then if they've both done the Sleep+Wait form then you still have a problem. You really only want some of the call sites (maybe just one) to use the Sleep+Wait form. I suppose that the production code will use time.Sleep since it's not importing synctest, so maybe it's clear that the test harness is the only one that will call Sleep+Wait. On the other hand, fixing a test failure by changing s/time.Sleep/synctest.Sleep/ will be a strange-looking bug fix. Better to have to add synctest.Wait instead. If we really need this, it could be synctest.SleepAndWait but that's what statements are for. Probably too subtle and should just limit the proposal to Run and Wait. |
Some additional suggestions for the description of the
Additionally, for "mutex operation", let's list out the the exact operations considered for implementation/testing completeness:
|
The API looks simple and that is excellent. What I am worried about is the unexpected failure modes, leading to undetected regressions, which might need tight support in the testing package to detect. Imagine you unit test your code but are unable to mock out a dependency. Maybe due to lack of experience or bad design of existing code I have to work with. That dependency that suddenly starts calling a syscall (e.g. to lazily try to tune the library using a sync.Once instead of on init time and having a timeout). Without support in testing you will never detect that now and only your tests will suddenly time out after an innocent minor dependency update. |
May I ortgogonally to the previous comment suggest to limit this package to standard library only to gather more experience with that approach before ? That would also allow to sketch out integration with the testing package in addition to finding more pitfalls. |
Can you expand more on what you mean by undetected regressions? If the code under test (either directly, or through a dependency) unexpectedly calls a blocking syscall,
What kind of support are you thinking of? |
What does this do?
Does it succeed or panic? It's not clear to me from the API docs because:
This is obviously a degenerate case, but I think it also applies if a test wanted to get the fake time features when testing otherwise non-concurrent code. |
In this case, the goroutine calling |
Edit: FWIW, I don't understand why this function is implemented this way. The select implementation ends up using |
@rsc suggested above #67434 (comment) that we restrict the panic behavior to timer channels. That would eliminate @prattmic 's concern. The updated proposal/CL applies the restriction to all bubbled channels, regardless of timers or data type. While the new update does solve the issue about buffering and (mostly) the issue about global channels, is there a good reason to do all channels instead of just timer channels? Or would time channels suffice for the intended testing cases? |
"Just timers" does seem better if we can make it work. Someone can implement what is semantically a mutex using (a) sync.Mutex, (b) channels, or (c) sync.Cond. |
To recap, the proposed panic behavior is: If a non-bubbled goroutine operates on a bubbled channel, panic. The rationale for having a distinction between bubbled and unbubbled channels is to allow a bubbled goroutine to access channel-synchronized resources from outside its bubble. For example, let's imagine a simple case where a channel is being used as a lock:
If we didn't have a distinction between bubbled and unbubbled channels, then a bubbled goroutine calling Get while the lock is held from outside the bubble will run into problems:
Get blocks writing to lockChan, the only goroutine in the bubble is now idle, Run panics because all goroutines are deadlocked. Making a distinction between bubbled and unbubbled channels means that instead when Get blocks on lockChan, synctest can recognize that it is blocked on something outside the bubble and not actually idle. If lockChan is lazily created, however, it might be inadvertently created within a synctest bubble. Now we fall back to the previous behavior: Some goroutine outside the bubble acquires lockChan, Get blocks on lockChan, lockChan is in the bubble, Run panics. But things are much more confusing than before, because the behavior depends on when lockChan was created. To avoid this confusion, we panic when the unbubbled goroutine writes to lockChan. An unbubbled goroutine accessing a bubbled channel indicates that something has gone wrong. The fix to the problem will probably be to ensure any lazy initialization happens outside the bubble. I think that if we distinguish between bubbled and unbubbled chans, then we need to prevent unbubbled goroutines from accessing bubbled chans to avoid confusion. If we don't distinguish between bubbled and unbubbled chans, then the overall model is simpler, but bubbled goroutines can't access global resources synchronized by a channel, which is unfortunate. |
I've greatly enjoyed using the preview of this addition. It's been very useful in my work. However, now that synctest is internal, it's become challenging to test and use outside stdlib. Would it be possible to make it accessible through a less restrictive means than a stdlib internal package? Perhaps a GOEXPERIMENT flag could work? The functionality is valuable enough that I'd consider vendoring it if it weren't so tightly coupled with Go's internals. This is such an awesome new addition. I'm eager to keep using it, even in an experimental state. :) |
Out of curiosity, I tried implementing the sync.Mutex semantics I described above: To be clear, I am not currently proposing we do this. This is just an experiment to see how intrusive the changes to sync.Mutex might be. To recap, this adds the following rules:
This essentially means that a mutex used within a bubble counts for idleness detection, but a global mutex shared with goroutines outside the bubble (such as the reflect type cache mutexes) does not. The changes required to sync.Mutex are not huge, but are not entirely trivial either. I'm still not convinced the value of mutex support in synctest is worth changing such a performance-critical type. |
I am not fully caught up on this, but my inclination is that we need to take a step back and rethink the concept of bubbled and non-bubbled synchronization primitives. Given that the heap is shared, it seems fundamental that synchronization between inside and outside a bubble needs to work without panicking. |
To summarize the current state of affairs as I understand them: The synctest package depends on identifying when bubbled goroutines are durably blocked. ("Durably": The goroutine is not just parked, it isn't going to unpark without some other goroutine in its bubble taking an action.) The synctest fake clock advances when all bubbled goroutines are durably blocked, and the Wait function lets tests wait for background work to complete. A bubbled goroutine can block on some non-bubbled resource. For example, reflect.TypeOf has a mutex-guarded cache, so a bubbled goroutine which calls TypeOf can block waiting for a non-bubbled goroutine. This goroutine is not durably blocked--it will resume executing when the non-bubbled goroutine releases the mutex. The reflect.TypeOf case demonstrates that synctest must gracefully handle the case of a bubbled goroutine non-durably blocked on a goroutine outside the bubble. We can impose limitations on what you can do inside a synctest bubble, but "don't call reflect.TypeOf" is too much of a limitation. There are three types of resource a bubbled goroutine can durably block on: Mutexes (sync.Mutex, sync.RWMutex), condition variables (sync.Cond), and channels. In all cases, a bubbled goroutine can block waiting for some other goroutine in the bubble, or block waiting for some global resource held by a goroutine outside the bubble. For a bubbled goroutine blocked on any of these resources, we can
In the last case, we distinguish between a resource (mutex, channel, cond) "in the bubble" and one "out of the bubble". (For efficiency and implementation simplicity, this probably takes the form of a boolean "in some bubble" state, rather than tracking the actual bubble, but that's an implementation detail.) A goroutine blocked on a mutex is always blocked because some goroutine acquired the mutex and has not released it. We could move a mutex into a bubble when a bubbled goroutine locks it, and move it out when it is unlocked. (This is demonstrated in https://go.dev/cl/613515.) This trick doesn't work for channels and conds. However, channels and conds are created with a constructor, so we could mark them as bubbled or non-bubbled at creation time. If resources have a bubbled/non-bubbled state, then there are several scenarios to consider:
This last case is the one where panicking can (and I think must) occur. If a bubbled goroutine has been marked durably idle, it should not be woken by some event outside the bubble--the entire notion of "durably idle" is that the goroutine is waiting only for events produced within the bubble. If we can mark channels as being bubbled, then it is an error for a bubbled channel to be operated on from outside the bubble, since a "bubbled channel" is specifically a channel that isn't supposed to escape its bubble. That's a lot of theory. I think the practical choices available to us are:
The simplest set of choices would be option 1 from all the above: blocking on a mutex is not idle, blocking on a chan or cond is always idle. That might be good enough, but it means synctest bubbles must not access global channels. That might be fine. (We can fix crypto/internal/randutil to not use a chan.) |
We had a VC discussion about how to progress. To summarize my understanding of our conclusions: For the moment, we're going to go with:
This is the approach currently implemented in https://go.dev/cl/613515. The rationale for these choices is based on what we're least likely to regret:
We will initially add the package with a GOEXPERIMENT=synctest guard to give people a chance to try out the API before we commit to it. (The package will only exist when using Go compiled with GOEXPERIMENT=synctest.) I will also pick out a few existing third-party modules that use fake clocks in tests, and try rewriting their tests to use synctest instead to provide some more examples of whether it provides any significant benefit compared to existing approaches to testing. |
@neild , just for logistics, would you mind creating a new mini proposal issue for landing this with your stated semantics as a GOEXPERIMENT? |
Done: #69687 |
I've encountered an unexpected problem caused by the new channel semantics (specifically: sending to a bubbled channel from outside the bubble panics). I have a net/http test which (highly simplified) does something like this:
(The actual test contains many more layers, of course, and the channel creation and t.Cleanup happen a couple levels of object initialization down.) Since the t.Cleanup func executes after synctest.Run returns, it is executed outside the bubble and the close(ch) operation panics. One obvious solution here is to say that I shouldn't do this. I can arrange for cleanup to happen inside the bubble. This is a bit unfortunate, however, since t.Cleanup really is tremendously useful and it would be a shame to take that tool off the shelf. Or perhaps this indicates that panicking when operating on a bubbled channel from outside the bubble is a mistake. I'm not convinced, though: Waking up an "idle" bubble from outside the bubble is never correct. On the other hand, in this particular case nothing in the bubble is being woken--the bubble is already gone. Another possibility is to add API to the testing package (as proposed by #67434 (comment) and others) to run a subtest in a bubble:
This could either be in addition to the proposed synctest package, or a complete replacement of it: synctest.Run becomes testing.T.RunIsolated, and synctest.Wait becomes (perhaps) testing.T.Wait. I am currently leaning towards saying that this case indicates that, while I'm reluctant to propose increasing the testing package's API surface, there needs to be a version of T.Run that starts a bubble and arranges for cleanup to happen within it. Probably on B and F as well for consistency, although I'm very dubious that any benchmark using a bubble will produce useful results. |
@neild why not |
@Merovius That's an interesting idea. Having a version of t.Run that isn't in the testing package seems a bit strange to me, but perhaps that would be okay. Implementation would be messy. I think the only way I see to do it is to implement the Run function in package testing and linkname it over to testing/synctest. The problem is that I think the messiness of implementation indicates that this isn't feasible. Maybe I'm missing a good way to do it. |
I'm going to propose two options: Option 1: Keep testing/synctest, add a method to testing.TB. We keep all of the existing proposal unchanged. We add one method in the testing package to T, B, F, and TB:
This option has the advantage of keeping all the complicated synctest documentation in its own package. Option 2: Move the API to the testing package. We drop the testing/synctest package and add the following to the testing package:
This option has the advantage of not adding a synctest.Run that, presumably, just about nobody will ever use (because testing.T.RunIsolated is the preferable choice), but it adds a big chunk of documentation to the testing package. Of the two, I lean towards option 2, but only slightly. |
Would it be possible to unbubble channels that are owned by a bubble when the bubble goes away, such that operating on them no longer panics after the bubble is ended? it might be too expensive to implement but I think it would logically solve this case in a clean way? |
Unbubbling channels after the bubble ends is an interesting idea. It would fix the panic in the case that I have. It wouldn't help a test that starts a background goroutine and signals that goroutine to exit in a t.Cleanup. (Think: Start a server that listens for connections in a separate goroutine, stop it in cleanup.) Implementation would be a bit tricky, since we need to store an association between channels and bubbles. The obvious and easy way is to put a pointer to the bubble on the channel, but that grows the size of a channel by a word; too expensive for a test-only feature. Alternatively, the bubble could have a set of weak references to bubbled channels, or we could have a global weak map of channel-to-bubble. |
This is a report on attempting to apply the proposed synctest API to existing, real-world code. etcd is a popular distributed key-value store. "github.com/jonboulle/clockwork" is an also-popular fake time package. The etcd repository (https://github.com/etcd-io/etcd) contains a number of Go modules, some of which use the clockwork package for testing. In this experiment, I rewrote the tests for package "go.etcd.io/etcd/server/v3/etcdserver/api/v3compactor" to use the proposed synctest API. To give away the ending: The synctest package was able to replace the fake clock, simplifying the system under test (SUT). The synctest package was also able to replace some complex manual synchronization between test and SUT, leading to a simpler, more robust test. In the following, I will go over one specific test in detail, explaining its behavior before and after my changes. The test is TestPeriodicPause: https://github.com/etcd-io/etcd/blob/ac3d5d77ea5fdbc12ef07a6f6fe1722f06d75b24/server/etcdserver/api/v3compactor/periodic_test.go#L132-L175 System Under TestThe system under test (SUT) is a Periodic. A Periodic operates on a RevGetter and a Compactable:
A Periodic maintains a background goroutine which periodically polls RevGetter.Rev, and calls Compactable.Compact when certain conditions are met. The internal details of RevGetter and Compactable are not important to the test, which uses a fake implementation of both. This is a straightforward system: The inputs are time and the revisions, and the output is a series of Compact calls. Test InfrastructureTestPeriodicPause uses a testutil.Recorder to synchronize and monitor the SUT.
A Recorder records a sequence of Actions (events) performed by the SUT. The fake implementations of RevGetter and Compactable record actions for each Rev/Compact call. The implementation of the Record interface is interesting and relevant to us. The testutil package contains two implementations of Recorder. A RecorderBuffered records each action to an internal slice of unbounded length. Record calls do not block. Wait calls attempt to wait for all pending record calls to finish before returning:
Note the comment about "legacy racey behavior", and the WaitSchedule function. A recorderStream, in contrast, records each action to an unbuffered channel. Record calls block until a Wait call consumes the action. A recorderStream is created with a timeout, where a timeout of 0 indicates no timeout. Wait(n) waits up to the timeout (or indefinitely when timeout==0) or until n actions are received. TestPeriodicPause uses blocking recorderStreams to synchronize with the SUT:
Note that the fakeRevGetter Recorder is created with no timeout, while the fakeCompatable Recorder has a 10ms timeout. The test uses the fakeRevGetter's Recorder to synchronize with the Periodic's background goroutine. For example, when advancing over an interval of time, the test reads actions from the RevGetter:
In each iteration of the loop above:
This is a complicated dance. Every call of RevGetter.Get by the SUT must be paired with a Recorder.Wait call by the test. It would be fairly easy to desynchronize the SUT and the test, with confusing results. However, the use of the Recorder to create synchronization points between the SUT and test allows tests to create carefully orchestrated scenarios with a fake clock. Using synctestUsing the synctest package, we can eliminate much of this test's infrastructure, and simplify what remained. I made the following changes:
The rewritten test:
And, for contrast, the original:
The complete code is at: What went wellThe synctest package was effective at providing synchronization and fake time for this test. The synctest version of the test is slightly shorter than the original, although some of that reduction in size is thanks to moving some functionality to the fakeCompactable type. The synctest version of the test is, I believe, easier to modify: The original depends on precisely pairing every recorded action in the SUT with a Wait call in the test. The original version of the test contains 10ms waits in various places, waiting for the SUT to stabilize. The synctest version just waits for the SUT to stabilize. What went less wellThe synctest.Run call indents all the test code by a level. Not really a big concern. I forgot to follow a time.Sleep call with a synctest.Wait a couple times. One test (TestRevisionPause) left a background goroutine running. This produces confusing results: The test hangs after executing, because synctest.Run keeps advancing the fake clock into the future and restarting the background goroutine. The fix was simple–stop the goroutine before finishing the test–but identifying the problem is a bit difficult. These last two points make me wonder if it would be clearer to have a function that explicitly advances the fake clock. Or perhaps we should stop advancing the fake clock when the root goroutine started by synctest.Run returns. |
Since a common error condition in tests is to still have Go routines running when one actually doesn't want to, identifying and nothing Go routines started but not exited would contribute to the isolation of this test feature. But that is probably its own proposal. When implementing synctest.Run as testing.T.RunIsolated, it would be great to keep that door open. |
If we want to keep |
We can have I'm fine with that, but it might be simpler to just say that if Run uses a *testing.T it belongs in the testing package. |
Personally I like the unbubbling idea, perhaps because I'm already a bit skeptical of the idea of bubbling in general. You could do it without increasing the size of chan by keeping a side table of bubbled channels.
It sounds like this should be easy to debug from a stack dump, no?
Does that mean that in a situation like the background goroutine, sleeps would block the goroutine forever? |
This is easy to debug from a stack dump--it's just briefly confusing, because the failure mode is for the test to hang indefinitely rather than giving you an immediate stack dump. Once I ran the test with -timeout=2s the stacks made it clear what had happened.
In the case with the background goroutine, the main test goroutine started by Run would return, and Run would then panic complaining that all remaining goroutines are blocked. (As opposed to advancing the fake clock, which is what it currently does.) I think we should do this; I'm not sure there's a use case for continuing to run the fake clock after the main test goroutine has returned, and we can change our minds later if we want. |
I've been thinking about this, and I think unbubbling channels at the end of the bubble isn't the right choice. The problem is that it's reasonable for a test to want to shut down background goroutines in a cleanup function. For example, a test may start a server listening on a fake network socket (with a background goroutine blocked in net.Listener.Accept) and stop the server in a cleanup function. If the cleanup function runs after the bubble exits, then it runs too late: a bubble never exits cleanly while any bubbled goroutines are still executing. Cleanup functions registered in a bubble should execute in the bubble. This is independent of the question of whether bubbling channels is a good idea or not--even if we don't associate channels with bubbles, we still want to be able to shut down a test completely before Run exits and its bubble ends. |
Putting on hold for experience with synctest under a GOEXPERIMENT (#69687). Discussion can of course continue, but this way we'll hold off on looking at this each week until there's more experience with it. |
This is a proposal for a new package to aid in testing concurrent code.
This package has two main features:
As an example, let us say we are testing an expiring concurrent cache:
A naive test for this cache might look something like this:
This test has a couple problems. It's slow, taking four seconds to execute. And it's flaky, because it assumes the cache entry will not have expired one second before its deadline and will have expired one second after. While computers are fast, it is not uncommon for an overloaded CI system to pause execution of a program for longer than a second.
We can make the test less flaky by making it slower, or we can make the test faster at the expense of making it flakier, but we can't make it fast and reliable using this approach.
We can design our Cache type to be more testable. We can inject a fake clock to give us control over time in tests. When advancing the fake clock, we will need some mechanism to ensure that any timers that fire have executed before progressing the test. These changes come at the expense of additional code complexity: We can no longer use time.Timer, but must use a testable wrapper. Background goroutines need additional synchronization points.
The synctest package simplifies all of this. Using synctest, we can write:
This is identical to the naive test above, wrapped in synctest.Run and with the addition of two calls to synctest.Wait. However:
A limitation of the synctest.Wait function is that it does not recognize goroutines blocked on network or other I/O operations as idle. While the scheduler can identify a goroutine blocked on I/O, it cannot distinguish between a goroutine that is genuinely blocked and one which is about to receive data from a kernel network buffer. For example, if a test creates a loopback TCP connection, starts a goroutine reading from one side of the connection, and then writes to the other, the read goroutine may remain in I/O wait for a brief time before the kernel indicates that the connection has become readable. If synctest.Wait considered a goroutine in I/O wait to be idle, this would cause nondeterminism in cases such as this,
Tests which use synctest with network connections or other external data sources should use a fake implementation with deterministic behavior. For net.Conn, net.Pipe can create a suitable in-memory connection.
This proposal is based in part on experience with tests in the golang.org/x/net/http2 package. Tests of an HTTP client or server often involve multiple interacting goroutines and timers. For example, a client request may involve goroutines writing to the server, reading from the server, and reading from the request body; as well as timers covering various stages of the request process. The combination of fake clocks and an operation which waits for all goroutines in the test to stabilize has proven effective.
The text was updated successfully, but these errors were encountered: