transfer_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. package xfer
  2. import (
  3. "sync/atomic"
  4. "testing"
  5. "time"
  6. "github.com/docker/docker/pkg/progress"
  7. )
  8. func TestTransfer(t *testing.T) {
  9. makeXferFunc := func(id string) DoFunc {
  10. return func(progressChan chan<- progress.Progress, start <-chan struct{}, inactive chan<- struct{}) Transfer {
  11. select {
  12. case <-start:
  13. default:
  14. t.Fatalf("transfer function not started even though concurrency limit not reached")
  15. }
  16. xfer := NewTransfer()
  17. go func() {
  18. for i := 0; i <= 10; i++ {
  19. progressChan <- progress.Progress{ID: id, Action: "testing", Current: int64(i), Total: 10}
  20. time.Sleep(10 * time.Millisecond)
  21. }
  22. close(progressChan)
  23. }()
  24. return xfer
  25. }
  26. }
  27. tm := NewTransferManager(5)
  28. progressChan := make(chan progress.Progress)
  29. progressDone := make(chan struct{})
  30. receivedProgress := make(map[string]int64)
  31. go func() {
  32. for p := range progressChan {
  33. val, present := receivedProgress[p.ID]
  34. if present && p.Current <= val {
  35. t.Fatalf("got unexpected progress value: %d (expected %d)", p.Current, val+1)
  36. }
  37. receivedProgress[p.ID] = p.Current
  38. }
  39. close(progressDone)
  40. }()
  41. // Start a few transfers
  42. ids := []string{"id1", "id2", "id3"}
  43. xfers := make([]Transfer, len(ids))
  44. watchers := make([]*Watcher, len(ids))
  45. for i, id := range ids {
  46. xfers[i], watchers[i] = tm.Transfer(id, makeXferFunc(id), progress.ChanOutput(progressChan))
  47. }
  48. for i, xfer := range xfers {
  49. <-xfer.Done()
  50. xfer.Release(watchers[i])
  51. }
  52. close(progressChan)
  53. <-progressDone
  54. for _, id := range ids {
  55. if receivedProgress[id] != 10 {
  56. t.Fatalf("final progress value %d instead of 10", receivedProgress[id])
  57. }
  58. }
  59. }
  60. func TestConcurrencyLimit(t *testing.T) {
  61. concurrencyLimit := 3
  62. var runningJobs int32
  63. makeXferFunc := func(id string) DoFunc {
  64. return func(progressChan chan<- progress.Progress, start <-chan struct{}, inactive chan<- struct{}) Transfer {
  65. xfer := NewTransfer()
  66. go func() {
  67. <-start
  68. totalJobs := atomic.AddInt32(&runningJobs, 1)
  69. if int(totalJobs) > concurrencyLimit {
  70. t.Fatalf("too many jobs running")
  71. }
  72. for i := 0; i <= 10; i++ {
  73. progressChan <- progress.Progress{ID: id, Action: "testing", Current: int64(i), Total: 10}
  74. time.Sleep(10 * time.Millisecond)
  75. }
  76. atomic.AddInt32(&runningJobs, -1)
  77. close(progressChan)
  78. }()
  79. return xfer
  80. }
  81. }
  82. tm := NewTransferManager(concurrencyLimit)
  83. progressChan := make(chan progress.Progress)
  84. progressDone := make(chan struct{})
  85. receivedProgress := make(map[string]int64)
  86. go func() {
  87. for p := range progressChan {
  88. receivedProgress[p.ID] = p.Current
  89. }
  90. close(progressDone)
  91. }()
  92. // Start more transfers than the concurrency limit
  93. ids := []string{"id1", "id2", "id3", "id4", "id5", "id6", "id7", "id8"}
  94. xfers := make([]Transfer, len(ids))
  95. watchers := make([]*Watcher, len(ids))
  96. for i, id := range ids {
  97. xfers[i], watchers[i] = tm.Transfer(id, makeXferFunc(id), progress.ChanOutput(progressChan))
  98. }
  99. for i, xfer := range xfers {
  100. <-xfer.Done()
  101. xfer.Release(watchers[i])
  102. }
  103. close(progressChan)
  104. <-progressDone
  105. for _, id := range ids {
  106. if receivedProgress[id] != 10 {
  107. t.Fatalf("final progress value %d instead of 10", receivedProgress[id])
  108. }
  109. }
  110. }
  111. func TestInactiveJobs(t *testing.T) {
  112. concurrencyLimit := 3
  113. var runningJobs int32
  114. testDone := make(chan struct{})
  115. makeXferFunc := func(id string) DoFunc {
  116. return func(progressChan chan<- progress.Progress, start <-chan struct{}, inactive chan<- struct{}) Transfer {
  117. xfer := NewTransfer()
  118. go func() {
  119. <-start
  120. totalJobs := atomic.AddInt32(&runningJobs, 1)
  121. if int(totalJobs) > concurrencyLimit {
  122. t.Fatalf("too many jobs running")
  123. }
  124. for i := 0; i <= 10; i++ {
  125. progressChan <- progress.Progress{ID: id, Action: "testing", Current: int64(i), Total: 10}
  126. time.Sleep(10 * time.Millisecond)
  127. }
  128. atomic.AddInt32(&runningJobs, -1)
  129. close(inactive)
  130. <-testDone
  131. close(progressChan)
  132. }()
  133. return xfer
  134. }
  135. }
  136. tm := NewTransferManager(concurrencyLimit)
  137. progressChan := make(chan progress.Progress)
  138. progressDone := make(chan struct{})
  139. receivedProgress := make(map[string]int64)
  140. go func() {
  141. for p := range progressChan {
  142. receivedProgress[p.ID] = p.Current
  143. }
  144. close(progressDone)
  145. }()
  146. // Start more transfers than the concurrency limit
  147. ids := []string{"id1", "id2", "id3", "id4", "id5", "id6", "id7", "id8"}
  148. xfers := make([]Transfer, len(ids))
  149. watchers := make([]*Watcher, len(ids))
  150. for i, id := range ids {
  151. xfers[i], watchers[i] = tm.Transfer(id, makeXferFunc(id), progress.ChanOutput(progressChan))
  152. }
  153. close(testDone)
  154. for i, xfer := range xfers {
  155. <-xfer.Done()
  156. xfer.Release(watchers[i])
  157. }
  158. close(progressChan)
  159. <-progressDone
  160. for _, id := range ids {
  161. if receivedProgress[id] != 10 {
  162. t.Fatalf("final progress value %d instead of 10", receivedProgress[id])
  163. }
  164. }
  165. }
  166. func TestWatchRelease(t *testing.T) {
  167. ready := make(chan struct{})
  168. makeXferFunc := func(id string) DoFunc {
  169. return func(progressChan chan<- progress.Progress, start <-chan struct{}, inactive chan<- struct{}) Transfer {
  170. xfer := NewTransfer()
  171. go func() {
  172. defer func() {
  173. close(progressChan)
  174. }()
  175. <-ready
  176. for i := int64(0); ; i++ {
  177. select {
  178. case <-time.After(10 * time.Millisecond):
  179. case <-xfer.Context().Done():
  180. return
  181. }
  182. progressChan <- progress.Progress{ID: id, Action: "testing", Current: i, Total: 10}
  183. }
  184. }()
  185. return xfer
  186. }
  187. }
  188. tm := NewTransferManager(5)
  189. type watcherInfo struct {
  190. watcher *Watcher
  191. progressChan chan progress.Progress
  192. progressDone chan struct{}
  193. receivedFirstProgress chan struct{}
  194. }
  195. progressConsumer := func(w watcherInfo) {
  196. first := true
  197. for range w.progressChan {
  198. if first {
  199. close(w.receivedFirstProgress)
  200. }
  201. first = false
  202. }
  203. close(w.progressDone)
  204. }
  205. // Start a transfer
  206. watchers := make([]watcherInfo, 5)
  207. var xfer Transfer
  208. watchers[0].progressChan = make(chan progress.Progress)
  209. watchers[0].progressDone = make(chan struct{})
  210. watchers[0].receivedFirstProgress = make(chan struct{})
  211. xfer, watchers[0].watcher = tm.Transfer("id1", makeXferFunc("id1"), progress.ChanOutput(watchers[0].progressChan))
  212. go progressConsumer(watchers[0])
  213. // Give it multiple watchers
  214. for i := 1; i != len(watchers); i++ {
  215. watchers[i].progressChan = make(chan progress.Progress)
  216. watchers[i].progressDone = make(chan struct{})
  217. watchers[i].receivedFirstProgress = make(chan struct{})
  218. watchers[i].watcher = xfer.Watch(progress.ChanOutput(watchers[i].progressChan))
  219. go progressConsumer(watchers[i])
  220. }
  221. // Now that the watchers are set up, allow the transfer goroutine to
  222. // proceed.
  223. close(ready)
  224. // Confirm that each watcher gets progress output.
  225. for _, w := range watchers {
  226. <-w.receivedFirstProgress
  227. }
  228. // Release one watcher every 5ms
  229. for _, w := range watchers {
  230. xfer.Release(w.watcher)
  231. <-time.After(5 * time.Millisecond)
  232. }
  233. // Now that all watchers have been released, Released() should
  234. // return a closed channel.
  235. <-xfer.Released()
  236. // Done() should return a closed channel because the xfer func returned
  237. // due to cancellation.
  238. <-xfer.Done()
  239. for _, w := range watchers {
  240. close(w.progressChan)
  241. <-w.progressDone
  242. }
  243. }
  244. func TestWatchFinishedTransfer(t *testing.T) {
  245. makeXferFunc := func(id string) DoFunc {
  246. return func(progressChan chan<- progress.Progress, start <-chan struct{}, inactive chan<- struct{}) Transfer {
  247. xfer := NewTransfer()
  248. go func() {
  249. // Finish immediately
  250. close(progressChan)
  251. }()
  252. return xfer
  253. }
  254. }
  255. tm := NewTransferManager(5)
  256. // Start a transfer
  257. watchers := make([]*Watcher, 3)
  258. var xfer Transfer
  259. xfer, watchers[0] = tm.Transfer("id1", makeXferFunc("id1"), progress.ChanOutput(make(chan progress.Progress)))
  260. // Give it a watcher immediately
  261. watchers[1] = xfer.Watch(progress.ChanOutput(make(chan progress.Progress)))
  262. // Wait for the transfer to complete
  263. <-xfer.Done()
  264. // Set up another watcher
  265. watchers[2] = xfer.Watch(progress.ChanOutput(make(chan progress.Progress)))
  266. // Release the watchers
  267. for _, w := range watchers {
  268. xfer.Release(w)
  269. }
  270. // Now that all watchers have been released, Released() should
  271. // return a closed channel.
  272. <-xfer.Released()
  273. }
  274. func TestDuplicateTransfer(t *testing.T) {
  275. ready := make(chan struct{})
  276. var xferFuncCalls int32
  277. makeXferFunc := func(id string) DoFunc {
  278. return func(progressChan chan<- progress.Progress, start <-chan struct{}, inactive chan<- struct{}) Transfer {
  279. atomic.AddInt32(&xferFuncCalls, 1)
  280. xfer := NewTransfer()
  281. go func() {
  282. defer func() {
  283. close(progressChan)
  284. }()
  285. <-ready
  286. for i := int64(0); ; i++ {
  287. select {
  288. case <-time.After(10 * time.Millisecond):
  289. case <-xfer.Context().Done():
  290. return
  291. }
  292. progressChan <- progress.Progress{ID: id, Action: "testing", Current: i, Total: 10}
  293. }
  294. }()
  295. return xfer
  296. }
  297. }
  298. tm := NewTransferManager(5)
  299. type transferInfo struct {
  300. xfer Transfer
  301. watcher *Watcher
  302. progressChan chan progress.Progress
  303. progressDone chan struct{}
  304. receivedFirstProgress chan struct{}
  305. }
  306. progressConsumer := func(t transferInfo) {
  307. first := true
  308. for range t.progressChan {
  309. if first {
  310. close(t.receivedFirstProgress)
  311. }
  312. first = false
  313. }
  314. close(t.progressDone)
  315. }
  316. // Try to start multiple transfers with the same ID
  317. transfers := make([]transferInfo, 5)
  318. for i := range transfers {
  319. t := &transfers[i]
  320. t.progressChan = make(chan progress.Progress)
  321. t.progressDone = make(chan struct{})
  322. t.receivedFirstProgress = make(chan struct{})
  323. t.xfer, t.watcher = tm.Transfer("id1", makeXferFunc("id1"), progress.ChanOutput(t.progressChan))
  324. go progressConsumer(*t)
  325. }
  326. // Allow the transfer goroutine to proceed.
  327. close(ready)
  328. // Confirm that each watcher gets progress output.
  329. for _, t := range transfers {
  330. <-t.receivedFirstProgress
  331. }
  332. // Confirm that the transfer function was called exactly once.
  333. if xferFuncCalls != 1 {
  334. t.Fatal("transfer function wasn't called exactly once")
  335. }
  336. // Release one watcher every 5ms
  337. for _, t := range transfers {
  338. t.xfer.Release(t.watcher)
  339. <-time.After(5 * time.Millisecond)
  340. }
  341. for _, t := range transfers {
  342. // Now that all watchers have been released, Released() should
  343. // return a closed channel.
  344. <-t.xfer.Released()
  345. // Done() should return a closed channel because the xfer func returned
  346. // due to cancellation.
  347. <-t.xfer.Done()
  348. }
  349. for _, t := range transfers {
  350. close(t.progressChan)
  351. <-t.progressDone
  352. }
  353. }