watch_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/internal/sync"
  21. "github.com/docker/compose/v2/pkg/api"
  22. "github.com/docker/compose/v2/pkg/mocks"
  23. "github.com/docker/compose/v2/pkg/watch"
  24. moby "github.com/docker/docker/api/types"
  25. "github.com/golang/mock/gomock"
  26. "github.com/jonboulle/clockwork"
  27. "github.com/stretchr/testify/require"
  28. "gotest.tools/v3/assert"
  29. )
  30. func TestDebounceBatching(t *testing.T) {
  31. ch := make(chan fileEvent)
  32. clock := clockwork.NewFakeClock()
  33. ctx, stop := context.WithCancel(context.Background())
  34. t.Cleanup(stop)
  35. eventBatchCh := batchDebounceEvents(ctx, clock, quietPeriod, ch)
  36. for i := 0; i < 100; i++ {
  37. var action WatchAction = "a"
  38. if i%2 == 0 {
  39. action = "b"
  40. }
  41. ch <- fileEvent{Action: action}
  42. }
  43. // we sent 100 events + the debouncer
  44. clock.BlockUntil(101)
  45. clock.Advance(quietPeriod)
  46. select {
  47. case batch := <-eventBatchCh:
  48. require.ElementsMatch(t, batch, []fileEvent{
  49. {Action: "a"},
  50. {Action: "b"},
  51. })
  52. case <-time.After(50 * time.Millisecond):
  53. t.Fatal("timed out waiting for events")
  54. }
  55. clock.BlockUntil(1)
  56. clock.Advance(quietPeriod)
  57. // there should only be a single batch
  58. select {
  59. case batch := <-eventBatchCh:
  60. t.Fatalf("unexpected events: %v", batch)
  61. case <-time.After(50 * time.Millisecond):
  62. // channel is empty
  63. }
  64. }
  65. type testWatcher struct {
  66. events chan watch.FileEvent
  67. errors chan error
  68. }
  69. func (t testWatcher) Start() error {
  70. return nil
  71. }
  72. func (t testWatcher) Close() error {
  73. return nil
  74. }
  75. func (t testWatcher) Events() chan watch.FileEvent {
  76. return t.events
  77. }
  78. func (t testWatcher) Errors() chan error {
  79. return t.errors
  80. }
  81. func TestWatch_Sync(t *testing.T) {
  82. mockCtrl := gomock.NewController(t)
  83. cli := mocks.NewMockCli(mockCtrl)
  84. cli.EXPECT().Err().Return(os.Stderr).AnyTimes()
  85. apiClient := mocks.NewMockAPIClient(mockCtrl)
  86. apiClient.EXPECT().ContainerList(gomock.Any(), gomock.Any()).Return([]moby.Container{
  87. testContainer("test", "123", false),
  88. }, nil).AnyTimes()
  89. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  90. ctx, cancelFunc := context.WithCancel(context.Background())
  91. t.Cleanup(cancelFunc)
  92. proj := types.Project{
  93. Services: []types.ServiceConfig{
  94. {
  95. Name: "test",
  96. },
  97. },
  98. }
  99. watcher := testWatcher{
  100. events: make(chan watch.FileEvent),
  101. errors: make(chan error),
  102. }
  103. syncer := newFakeSyncer()
  104. clock := clockwork.NewFakeClock()
  105. go func() {
  106. service := composeService{
  107. dockerCli: cli,
  108. clock: clock,
  109. }
  110. err := service.watch(ctx, &proj, "test", api.WatchOptions{}, watcher, syncer, []Trigger{
  111. {
  112. Path: "/sync",
  113. Action: "sync",
  114. Target: "/work",
  115. Ignore: []string{"ignore"},
  116. },
  117. {
  118. Path: "/rebuild",
  119. Action: "rebuild",
  120. },
  121. })
  122. assert.NilError(t, err)
  123. }()
  124. watcher.Events() <- watch.NewFileEvent("/sync/changed")
  125. watcher.Events() <- watch.NewFileEvent("/sync/changed/sub")
  126. clock.BlockUntil(3)
  127. clock.Advance(quietPeriod)
  128. select {
  129. case actual := <-syncer.synced:
  130. require.ElementsMatch(t, []sync.PathMapping{
  131. {HostPath: "/sync/changed", ContainerPath: "/work/changed"},
  132. {HostPath: "/sync/changed/sub", ContainerPath: "/work/changed/sub"},
  133. }, actual)
  134. case <-time.After(100 * time.Millisecond):
  135. t.Error("timeout")
  136. }
  137. watcher.Events() <- watch.NewFileEvent("/sync/ignore")
  138. watcher.Events() <- watch.NewFileEvent("/sync/ignore/sub")
  139. watcher.Events() <- watch.NewFileEvent("/sync/changed")
  140. clock.BlockUntil(4)
  141. clock.Advance(quietPeriod)
  142. select {
  143. case actual := <-syncer.synced:
  144. require.ElementsMatch(t, []sync.PathMapping{
  145. {HostPath: "/sync/changed", ContainerPath: "/work/changed"},
  146. }, actual)
  147. case <-time.After(100 * time.Millisecond):
  148. t.Error("timed out waiting for events")
  149. }
  150. watcher.Events() <- watch.NewFileEvent("/rebuild")
  151. watcher.Events() <- watch.NewFileEvent("/sync/changed")
  152. clock.BlockUntil(4)
  153. clock.Advance(quietPeriod)
  154. select {
  155. case batch := <-syncer.synced:
  156. t.Fatalf("received unexpected events: %v", batch)
  157. case <-time.After(100 * time.Millisecond):
  158. // expected
  159. }
  160. // TODO: there's not a great way to assert that the rebuild attempt happened
  161. }
  162. type fakeSyncer struct {
  163. synced chan []sync.PathMapping
  164. }
  165. func newFakeSyncer() *fakeSyncer {
  166. return &fakeSyncer{
  167. synced: make(chan []sync.PathMapping),
  168. }
  169. }
  170. func (f *fakeSyncer) Sync(_ context.Context, _ types.ServiceConfig, paths []sync.PathMapping) error {
  171. f.synced <- paths
  172. return nil
  173. }