mirror of
https://github.com/docker/compose.git
synced 2026-10-12 02:13:08 +00:00
The fake clock is only used by tests to control the debounce timer, but requires carrying a clock abstraction through production code. Use the standard time package for the debounce ticker and use testing/synctest to control time in tests instead. This removes the clock from composeService and BatchDebounceEvents, and eliminates the clockwork dependency from the watch implementation. Signed-off-by: Sebastiaan van Stijn <[email protected]>
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
/*
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package watch
|
|
|
|
import (
|
|
"slices"
|
|
"testing"
|
|
"testing/synctest"
|
|
"time"
|
|
|
|
"gotest.tools/v3/assert"
|
|
)
|
|
|
|
func Test_BatchDebounceEvents(t *testing.T) {
|
|
synctest.Test(t, func(t *testing.T) {
|
|
ch := make(chan FileEvent)
|
|
|
|
eventBatchCh := BatchDebounceEvents(t.Context(), ch)
|
|
for i := range 100 {
|
|
path := "/a"
|
|
if i%2 == 0 {
|
|
path = "/b"
|
|
}
|
|
|
|
ch <- FileEvent(path)
|
|
}
|
|
time.Sleep(QuietPeriod)
|
|
synctest.Wait()
|
|
batch := <-eventBatchCh
|
|
slices.Sort(batch)
|
|
assert.Equal(t, len(batch), 2)
|
|
assert.Equal(t, batch[0], FileEvent("/a"))
|
|
assert.Equal(t, batch[1], FileEvent("/b"))
|
|
|
|
time.Sleep(QuietPeriod)
|
|
synctest.Wait()
|
|
|
|
// there should only be a single batch
|
|
select {
|
|
case batch := <-eventBatchCh:
|
|
t.Fatalf("unexpected events: %v", batch)
|
|
default:
|
|
// channel is empty
|
|
}
|
|
})
|
|
}
|