watch_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "os"
  17. "testing"
  18. "time"
  19. "github.com/compose-spec/compose-go/types"
  20. "github.com/docker/compose/v2/pkg/mocks"
  21. moby "github.com/docker/docker/api/types"
  22. "github.com/golang/mock/gomock"
  23. "github.com/jonboulle/clockwork"
  24. "github.com/stretchr/testify/require"
  25. "github.com/docker/compose/v2/internal/sync"
  26. "github.com/docker/compose/v2/pkg/watch"
  27. "gotest.tools/v3/assert"
  28. )
  29. func TestDebounceBatching(t *testing.T) {
  30. ch := make(chan fileEvent)
  31. clock := clockwork.NewFakeClock()
  32. ctx, stop := context.WithCancel(context.Background())
  33. t.Cleanup(stop)
  34. eventBatchCh := batchDebounceEvents(ctx, clock, quietPeriod, ch)
  35. for i := 0; i < 100; i++ {
  36. var action WatchAction = "a"
  37. if i%2 == 0 {
  38. action = "b"
  39. }
  40. ch <- fileEvent{Action: action}
  41. }
  42. // we sent 100 events + the debouncer
  43. clock.BlockUntil(101)
  44. clock.Advance(quietPeriod)
  45. select {
  46. case batch := <-eventBatchCh:
  47. require.ElementsMatch(t, batch, []fileEvent{
  48. {Action: "a"},
  49. {Action: "b"},
  50. })
  51. case <-time.After(50 * time.Millisecond):
  52. t.Fatal("timed out waiting for events")
  53. }
  54. clock.BlockUntil(1)
  55. clock.Advance(quietPeriod)
  56. // there should only be a single batch
  57. select {
  58. case batch := <-eventBatchCh:
  59. t.Fatalf("unexpected events: %v", batch)
  60. case <-time.After(50 * time.Millisecond):
  61. // channel is empty
  62. }
  63. }
  64. type testWatcher struct {
  65. events chan watch.FileEvent
  66. errors chan error
  67. }
  68. func (t testWatcher) Start() error {
  69. return nil
  70. }
  71. func (t testWatcher) Close() error {
  72. return nil
  73. }
  74. func (t testWatcher) Events() chan watch.FileEvent {
  75. return t.events
  76. }
  77. func (t testWatcher) Errors() chan error {
  78. return t.errors
  79. }
  80. func TestWatch_Sync(t *testing.T) {
  81. mockCtrl := gomock.NewController(t)
  82. cli := mocks.NewMockCli(mockCtrl)
  83. cli.EXPECT().Err().Return(os.Stderr).AnyTimes()
  84. apiClient := mocks.NewMockAPIClient(mockCtrl)
  85. apiClient.EXPECT().ContainerList(gomock.Any(), gomock.Any()).Return([]moby.Container{
  86. testContainer("test", "123", false),
  87. }, nil).AnyTimes()
  88. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  89. ctx, cancelFunc := context.WithCancel(context.Background())
  90. t.Cleanup(cancelFunc)
  91. proj := types.Project{
  92. Services: []types.ServiceConfig{
  93. {
  94. Name: "test",
  95. },
  96. },
  97. }
  98. watcher := testWatcher{
  99. events: make(chan watch.FileEvent),
  100. errors: make(chan error),
  101. }
  102. syncer := newFakeSyncer()
  103. clock := clockwork.NewFakeClock()
  104. go func() {
  105. service := composeService{
  106. dockerCli: cli,
  107. clock: clock,
  108. }
  109. err := service.watch(ctx, &proj, "test", watcher, syncer, []Trigger{
  110. {
  111. Path: "/sync",
  112. Action: "sync",
  113. Target: "/work",
  114. Ignore: []string{"ignore"},
  115. },
  116. {
  117. Path: "/rebuild",
  118. Action: "rebuild",
  119. },
  120. })
  121. assert.NilError(t, err)
  122. }()
  123. watcher.Events() <- watch.NewFileEvent("/sync/changed")
  124. watcher.Events() <- watch.NewFileEvent("/sync/changed/sub")
  125. clock.BlockUntil(3)
  126. clock.Advance(quietPeriod)
  127. select {
  128. case actual := <-syncer.synced:
  129. require.ElementsMatch(t, []sync.PathMapping{
  130. {HostPath: "/sync/changed", ContainerPath: "/work/changed"},
  131. {HostPath: "/sync/changed/sub", ContainerPath: "/work/changed/sub"},
  132. }, actual)
  133. case <-time.After(100 * time.Millisecond):
  134. t.Error("timeout")
  135. }
  136. watcher.Events() <- watch.NewFileEvent("/sync/ignore")
  137. watcher.Events() <- watch.NewFileEvent("/sync/ignore/sub")
  138. watcher.Events() <- watch.NewFileEvent("/sync/changed")
  139. clock.BlockUntil(4)
  140. clock.Advance(quietPeriod)
  141. select {
  142. case actual := <-syncer.synced:
  143. require.ElementsMatch(t, []sync.PathMapping{
  144. {HostPath: "/sync/changed", ContainerPath: "/work/changed"},
  145. }, actual)
  146. case <-time.After(100 * time.Millisecond):
  147. t.Error("timed out waiting for events")
  148. }
  149. watcher.Events() <- watch.NewFileEvent("/rebuild")
  150. watcher.Events() <- watch.NewFileEvent("/sync/changed")
  151. clock.BlockUntil(4)
  152. clock.Advance(quietPeriod)
  153. select {
  154. case batch := <-syncer.synced:
  155. t.Fatalf("received unexpected events: %v", batch)
  156. case <-time.After(100 * time.Millisecond):
  157. // expected
  158. }
  159. // TODO: there's not a great way to assert that the rebuild attempt happened
  160. }
  161. type fakeSyncer struct {
  162. synced chan []sync.PathMapping
  163. }
  164. func newFakeSyncer() *fakeSyncer {
  165. return &fakeSyncer{
  166. synced: make(chan []sync.PathMapping),
  167. }
  168. }
  169. func (f *fakeSyncer) Sync(_ context.Context, _ types.ServiceConfig, paths []sync.PathMapping) error {
  170. f.synced <- paths
  171. return nil
  172. }