watch_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. "testing"
  17. "time"
  18. "github.com/docker/compose/v2/internal/sync"
  19. "github.com/docker/cli/cli/command"
  20. "github.com/docker/compose/v2/pkg/watch"
  21. "github.com/jonboulle/clockwork"
  22. "golang.org/x/sync/errgroup"
  23. "gotest.tools/v3/assert"
  24. )
  25. func Test_debounce(t *testing.T) {
  26. ch := make(chan fileEvent)
  27. var (
  28. ran int
  29. got []string
  30. )
  31. clock := clockwork.NewFakeClock()
  32. ctx, stop := context.WithCancel(context.Background())
  33. t.Cleanup(stop)
  34. eg, ctx := errgroup.WithContext(ctx)
  35. eg.Go(func() error {
  36. debounce(ctx, clock, quietPeriod, ch, func(services rebuildServices) {
  37. for svc := range services {
  38. got = append(got, svc)
  39. }
  40. ran++
  41. stop()
  42. })
  43. return nil
  44. })
  45. for i := 0; i < 100; i++ {
  46. ch <- fileEvent{Service: "test"}
  47. }
  48. assert.Equal(t, ran, 0)
  49. clock.Advance(quietPeriod)
  50. err := eg.Wait()
  51. assert.NilError(t, err)
  52. assert.Equal(t, ran, 1)
  53. assert.DeepEqual(t, got, []string{"test"})
  54. }
  55. type testWatcher struct {
  56. events chan watch.FileEvent
  57. errors chan error
  58. }
  59. func (t testWatcher) Start() error {
  60. return nil
  61. }
  62. func (t testWatcher) Close() error {
  63. return nil
  64. }
  65. func (t testWatcher) Events() chan watch.FileEvent {
  66. return t.events
  67. }
  68. func (t testWatcher) Errors() chan error {
  69. return t.errors
  70. }
  71. func Test_sync(t *testing.T) {
  72. needSync := make(chan sync.PathMapping)
  73. needRebuild := make(chan fileEvent)
  74. ctx, cancelFunc := context.WithCancel(context.TODO())
  75. defer cancelFunc()
  76. run := func() watch.Notify {
  77. watcher := testWatcher{
  78. events: make(chan watch.FileEvent, 1),
  79. errors: make(chan error),
  80. }
  81. go func() {
  82. cli, err := command.NewDockerCli()
  83. assert.NilError(t, err)
  84. service := composeService{
  85. dockerCli: cli,
  86. }
  87. err = service.watch(ctx, "test", watcher, []Trigger{
  88. {
  89. Path: "/src",
  90. Action: "sync",
  91. Target: "/work",
  92. Ignore: []string{"ignore"},
  93. },
  94. {
  95. Path: "/",
  96. Action: "rebuild",
  97. },
  98. }, needSync, needRebuild)
  99. assert.NilError(t, err)
  100. }()
  101. return watcher
  102. }
  103. t.Run("synchronize file", func(t *testing.T) {
  104. watcher := run()
  105. watcher.Events() <- watch.NewFileEvent("/src/changed")
  106. select {
  107. case actual := <-needSync:
  108. assert.DeepEqual(t, sync.PathMapping{Service: "test", HostPath: "/src/changed", ContainerPath: "/work/changed"}, actual)
  109. case <-time.After(100 * time.Millisecond):
  110. t.Error("timeout")
  111. }
  112. })
  113. t.Run("ignore", func(t *testing.T) {
  114. watcher := run()
  115. watcher.Events() <- watch.NewFileEvent("/src/ignore")
  116. select {
  117. case <-needSync:
  118. t.Error("file event should have been ignored")
  119. case <-time.After(100 * time.Millisecond):
  120. // expected
  121. }
  122. })
  123. t.Run("rebuild", func(t *testing.T) {
  124. watcher := run()
  125. watcher.Events() <- watch.NewFileEvent("/dependencies.yaml")
  126. select {
  127. case event := <-needRebuild:
  128. assert.Equal(t, "test", event.Service)
  129. case <-time.After(100 * time.Millisecond):
  130. t.Error("timeout")
  131. }
  132. })
  133. }