watch_test.go 3.4 KB

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