2018-02-05 16:05:59 -05:00
|
|
|
package xfer // import "github.com/docker/docker/distribution/xfer"
|
2015-11-13 19:59:01 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/progress"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTransfer(t *testing.T) {
|
2022-02-18 10:54:12 -05:00
|
|
|
makeXferFunc := func(id string) doFunc {
|
2022-01-22 07:11:24 -05:00
|
|
|
return func(progressChan chan<- progress.Progress, start <-chan struct{}, _ chan<- struct{}) transfer {
|
2015-11-13 19:59:01 -05:00
|
|
|
select {
|
|
|
|
case <-start:
|
|
|
|
default:
|
TestTransfer*: don't call t.Fatal from a goroutine
staticcheck go linter warns:
> distribution/xfer/transfer_test.go:37:2: SA2002: the goroutine calls T.Fatalf, which must be called in the same goroutine as the test (staticcheck)
What it doesn't say is why. The reason is, t.Fatalf() calls t.FailNow(),
which is expected to stop test execution right now. It does so by
calling runtime.Goexit(), which, unless called from a main goroutine,
does not stop test execution.
Anyway, long story short, if we don't care much about stopping the test
case immediately, we can just replace t.Fatalf() with t.Errorf() which
still marks the test case as failed, but won't stop it immediately.
This patch was tested to check that the test fails if any of the
goroutines call t.Errorf():
1. Failure in DoFunc ("transfer function not started ...") was tested by
decreading the NewTransferManager() argument:
- tm := NewTransferManager(5)
+ tm := NewTransferManager(2)
2. Failure "got unexpected progress value" was tested by injecting a random:
- if present && p.Current <= val {
+ if present && p.Current <= val || rand.Intn(100) > 80 {
3. Failure in DoFunc ("too many jobs running") was tested by increasing
the NewTransferManager() argument:
- tm := NewTransferManager(concurrencyLimit)
+ tm := NewTransferManager(concurrencyLimit + 1)
While at it:
* fix/amend some error messages
* use _ for unused arguments of DoFunc
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2019-08-06 18:37:07 -04:00
|
|
|
t.Errorf("%s: transfer function not started even though concurrency limit not reached", id)
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
2022-01-22 07:04:46 -05:00
|
|
|
xfer := newTransfer()
|
2015-11-13 19:59:01 -05:00
|
|
|
go func() {
|
|
|
|
for i := 0; i <= 10; i++ {
|
|
|
|
progressChan <- progress.Progress{ID: id, Action: "testing", Current: int64(i), Total: 10}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
close(progressChan)
|
|
|
|
}()
|
|
|
|
return xfer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 06:19:42 -05:00
|
|
|
tm := newTransferManager(5)
|
2015-11-13 19:59:01 -05:00
|
|
|
progressChan := make(chan progress.Progress)
|
|
|
|
progressDone := make(chan struct{})
|
|
|
|
receivedProgress := make(map[string]int64)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for p := range progressChan {
|
|
|
|
val, present := receivedProgress[p.ID]
|
2016-03-28 21:27:29 -04:00
|
|
|
if present && p.Current <= val {
|
TestTransfer*: don't call t.Fatal from a goroutine
staticcheck go linter warns:
> distribution/xfer/transfer_test.go:37:2: SA2002: the goroutine calls T.Fatalf, which must be called in the same goroutine as the test (staticcheck)
What it doesn't say is why. The reason is, t.Fatalf() calls t.FailNow(),
which is expected to stop test execution right now. It does so by
calling runtime.Goexit(), which, unless called from a main goroutine,
does not stop test execution.
Anyway, long story short, if we don't care much about stopping the test
case immediately, we can just replace t.Fatalf() with t.Errorf() which
still marks the test case as failed, but won't stop it immediately.
This patch was tested to check that the test fails if any of the
goroutines call t.Errorf():
1. Failure in DoFunc ("transfer function not started ...") was tested by
decreading the NewTransferManager() argument:
- tm := NewTransferManager(5)
+ tm := NewTransferManager(2)
2. Failure "got unexpected progress value" was tested by injecting a random:
- if present && p.Current <= val {
+ if present && p.Current <= val || rand.Intn(100) > 80 {
3. Failure in DoFunc ("too many jobs running") was tested by increasing
the NewTransferManager() argument:
- tm := NewTransferManager(concurrencyLimit)
+ tm := NewTransferManager(concurrencyLimit + 1)
While at it:
* fix/amend some error messages
* use _ for unused arguments of DoFunc
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2019-08-06 18:37:07 -04:00
|
|
|
t.Errorf("%s: got unexpected progress value: %d (expected <= %d)", p.ID, p.Current, val)
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
receivedProgress[p.ID] = p.Current
|
|
|
|
}
|
|
|
|
close(progressDone)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Start a few transfers
|
|
|
|
ids := []string{"id1", "id2", "id3"}
|
2022-01-22 07:11:24 -05:00
|
|
|
xfers := make([]transfer, len(ids))
|
2022-01-22 08:32:18 -05:00
|
|
|
watchers := make([]*watcher, len(ids))
|
2015-11-13 19:59:01 -05:00
|
|
|
for i, id := range ids {
|
2022-01-22 06:40:35 -05:00
|
|
|
xfers[i], watchers[i] = tm.transfer(id, makeXferFunc(id), progress.ChanOutput(progressChan))
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, xfer := range xfers {
|
2022-02-18 08:03:35 -05:00
|
|
|
<-xfer.done()
|
|
|
|
xfer.release(watchers[i])
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
close(progressChan)
|
|
|
|
<-progressDone
|
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
if receivedProgress[id] != 10 {
|
|
|
|
t.Fatalf("final progress value %d instead of 10", receivedProgress[id])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConcurrencyLimit(t *testing.T) {
|
2022-01-21 13:55:49 -05:00
|
|
|
const concurrencyLimit = 3
|
2015-11-13 19:59:01 -05:00
|
|
|
var runningJobs int32
|
|
|
|
|
2022-02-18 10:54:12 -05:00
|
|
|
makeXferFunc := func(id string) doFunc {
|
2022-01-22 07:11:24 -05:00
|
|
|
return func(progressChan chan<- progress.Progress, start <-chan struct{}, _ chan<- struct{}) transfer {
|
2022-01-22 07:04:46 -05:00
|
|
|
xfer := newTransfer()
|
2015-11-13 19:59:01 -05:00
|
|
|
go func() {
|
|
|
|
<-start
|
|
|
|
totalJobs := atomic.AddInt32(&runningJobs, 1)
|
|
|
|
if int(totalJobs) > concurrencyLimit {
|
TestTransfer*: don't call t.Fatal from a goroutine
staticcheck go linter warns:
> distribution/xfer/transfer_test.go:37:2: SA2002: the goroutine calls T.Fatalf, which must be called in the same goroutine as the test (staticcheck)
What it doesn't say is why. The reason is, t.Fatalf() calls t.FailNow(),
which is expected to stop test execution right now. It does so by
calling runtime.Goexit(), which, unless called from a main goroutine,
does not stop test execution.
Anyway, long story short, if we don't care much about stopping the test
case immediately, we can just replace t.Fatalf() with t.Errorf() which
still marks the test case as failed, but won't stop it immediately.
This patch was tested to check that the test fails if any of the
goroutines call t.Errorf():
1. Failure in DoFunc ("transfer function not started ...") was tested by
decreading the NewTransferManager() argument:
- tm := NewTransferManager(5)
+ tm := NewTransferManager(2)
2. Failure "got unexpected progress value" was tested by injecting a random:
- if present && p.Current <= val {
+ if present && p.Current <= val || rand.Intn(100) > 80 {
3. Failure in DoFunc ("too many jobs running") was tested by increasing
the NewTransferManager() argument:
- tm := NewTransferManager(concurrencyLimit)
+ tm := NewTransferManager(concurrencyLimit + 1)
While at it:
* fix/amend some error messages
* use _ for unused arguments of DoFunc
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2019-08-06 18:37:07 -04:00
|
|
|
t.Errorf("%s: too many jobs running (%d > %d)", id, totalJobs, concurrencyLimit)
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
for i := 0; i <= 10; i++ {
|
|
|
|
progressChan <- progress.Progress{ID: id, Action: "testing", Current: int64(i), Total: 10}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
atomic.AddInt32(&runningJobs, -1)
|
|
|
|
close(progressChan)
|
|
|
|
}()
|
|
|
|
return xfer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 06:19:42 -05:00
|
|
|
tm := newTransferManager(concurrencyLimit)
|
2015-11-13 19:59:01 -05:00
|
|
|
progressChan := make(chan progress.Progress)
|
|
|
|
progressDone := make(chan struct{})
|
|
|
|
receivedProgress := make(map[string]int64)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for p := range progressChan {
|
|
|
|
receivedProgress[p.ID] = p.Current
|
|
|
|
}
|
|
|
|
close(progressDone)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Start more transfers than the concurrency limit
|
|
|
|
ids := []string{"id1", "id2", "id3", "id4", "id5", "id6", "id7", "id8"}
|
2022-01-22 07:11:24 -05:00
|
|
|
xfers := make([]transfer, len(ids))
|
2022-01-22 08:32:18 -05:00
|
|
|
watchers := make([]*watcher, len(ids))
|
2015-11-13 19:59:01 -05:00
|
|
|
for i, id := range ids {
|
2022-01-22 06:40:35 -05:00
|
|
|
xfers[i], watchers[i] = tm.transfer(id, makeXferFunc(id), progress.ChanOutput(progressChan))
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, xfer := range xfers {
|
2022-02-18 08:03:35 -05:00
|
|
|
<-xfer.done()
|
|
|
|
xfer.release(watchers[i])
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
close(progressChan)
|
|
|
|
<-progressDone
|
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
if receivedProgress[id] != 10 {
|
|
|
|
t.Fatalf("final progress value %d instead of 10", receivedProgress[id])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInactiveJobs(t *testing.T) {
|
2022-01-21 13:55:49 -05:00
|
|
|
const concurrencyLimit = 3
|
2015-11-13 19:59:01 -05:00
|
|
|
var runningJobs int32
|
|
|
|
testDone := make(chan struct{})
|
|
|
|
|
2022-02-18 10:54:12 -05:00
|
|
|
makeXferFunc := func(id string) doFunc {
|
2022-01-22 07:11:24 -05:00
|
|
|
return func(progressChan chan<- progress.Progress, start <-chan struct{}, inactive chan<- struct{}) transfer {
|
2022-01-22 07:04:46 -05:00
|
|
|
xfer := newTransfer()
|
2015-11-13 19:59:01 -05:00
|
|
|
go func() {
|
|
|
|
<-start
|
|
|
|
totalJobs := atomic.AddInt32(&runningJobs, 1)
|
|
|
|
if int(totalJobs) > concurrencyLimit {
|
TestTransfer*: don't call t.Fatal from a goroutine
staticcheck go linter warns:
> distribution/xfer/transfer_test.go:37:2: SA2002: the goroutine calls T.Fatalf, which must be called in the same goroutine as the test (staticcheck)
What it doesn't say is why. The reason is, t.Fatalf() calls t.FailNow(),
which is expected to stop test execution right now. It does so by
calling runtime.Goexit(), which, unless called from a main goroutine,
does not stop test execution.
Anyway, long story short, if we don't care much about stopping the test
case immediately, we can just replace t.Fatalf() with t.Errorf() which
still marks the test case as failed, but won't stop it immediately.
This patch was tested to check that the test fails if any of the
goroutines call t.Errorf():
1. Failure in DoFunc ("transfer function not started ...") was tested by
decreading the NewTransferManager() argument:
- tm := NewTransferManager(5)
+ tm := NewTransferManager(2)
2. Failure "got unexpected progress value" was tested by injecting a random:
- if present && p.Current <= val {
+ if present && p.Current <= val || rand.Intn(100) > 80 {
3. Failure in DoFunc ("too many jobs running") was tested by increasing
the NewTransferManager() argument:
- tm := NewTransferManager(concurrencyLimit)
+ tm := NewTransferManager(concurrencyLimit + 1)
While at it:
* fix/amend some error messages
* use _ for unused arguments of DoFunc
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2019-08-06 18:37:07 -04:00
|
|
|
t.Errorf("%s: too many jobs running (%d > %d)", id, totalJobs, concurrencyLimit)
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
for i := 0; i <= 10; i++ {
|
|
|
|
progressChan <- progress.Progress{ID: id, Action: "testing", Current: int64(i), Total: 10}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
atomic.AddInt32(&runningJobs, -1)
|
|
|
|
close(inactive)
|
|
|
|
<-testDone
|
|
|
|
close(progressChan)
|
|
|
|
}()
|
|
|
|
return xfer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 06:19:42 -05:00
|
|
|
tm := newTransferManager(concurrencyLimit)
|
2015-11-13 19:59:01 -05:00
|
|
|
progressChan := make(chan progress.Progress)
|
|
|
|
progressDone := make(chan struct{})
|
|
|
|
receivedProgress := make(map[string]int64)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for p := range progressChan {
|
|
|
|
receivedProgress[p.ID] = p.Current
|
|
|
|
}
|
|
|
|
close(progressDone)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Start more transfers than the concurrency limit
|
|
|
|
ids := []string{"id1", "id2", "id3", "id4", "id5", "id6", "id7", "id8"}
|
2022-01-22 07:11:24 -05:00
|
|
|
xfers := make([]transfer, len(ids))
|
2022-01-22 08:32:18 -05:00
|
|
|
watchers := make([]*watcher, len(ids))
|
2015-11-13 19:59:01 -05:00
|
|
|
for i, id := range ids {
|
2022-01-22 06:40:35 -05:00
|
|
|
xfers[i], watchers[i] = tm.transfer(id, makeXferFunc(id), progress.ChanOutput(progressChan))
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
close(testDone)
|
|
|
|
for i, xfer := range xfers {
|
2022-02-18 08:03:35 -05:00
|
|
|
<-xfer.done()
|
|
|
|
xfer.release(watchers[i])
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
close(progressChan)
|
|
|
|
<-progressDone
|
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
if receivedProgress[id] != 10 {
|
|
|
|
t.Fatalf("final progress value %d instead of 10", receivedProgress[id])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatchRelease(t *testing.T) {
|
|
|
|
ready := make(chan struct{})
|
|
|
|
|
2022-02-18 10:54:12 -05:00
|
|
|
makeXferFunc := func(id string) doFunc {
|
2022-01-22 07:11:24 -05:00
|
|
|
return func(progressChan chan<- progress.Progress, start <-chan struct{}, _ chan<- struct{}) transfer {
|
2022-01-22 07:04:46 -05:00
|
|
|
xfer := newTransfer()
|
2015-11-13 19:59:01 -05:00
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
close(progressChan)
|
|
|
|
}()
|
|
|
|
<-ready
|
|
|
|
for i := int64(0); ; i++ {
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
2022-02-18 08:03:35 -05:00
|
|
|
case <-xfer.context().Done():
|
2015-11-13 19:59:01 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
progressChan <- progress.Progress{ID: id, Action: "testing", Current: i, Total: 10}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return xfer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 06:19:42 -05:00
|
|
|
tm := newTransferManager(5)
|
2015-11-13 19:59:01 -05:00
|
|
|
|
|
|
|
type watcherInfo struct {
|
2022-01-22 08:32:18 -05:00
|
|
|
watcher *watcher
|
2015-11-13 19:59:01 -05:00
|
|
|
progressChan chan progress.Progress
|
|
|
|
progressDone chan struct{}
|
|
|
|
receivedFirstProgress chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
progressConsumer := func(w watcherInfo) {
|
|
|
|
first := true
|
|
|
|
for range w.progressChan {
|
|
|
|
if first {
|
|
|
|
close(w.receivedFirstProgress)
|
|
|
|
}
|
|
|
|
first = false
|
|
|
|
}
|
|
|
|
close(w.progressDone)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a transfer
|
|
|
|
watchers := make([]watcherInfo, 5)
|
2022-01-22 07:11:24 -05:00
|
|
|
var xfer transfer
|
2015-11-13 19:59:01 -05:00
|
|
|
watchers[0].progressChan = make(chan progress.Progress)
|
|
|
|
watchers[0].progressDone = make(chan struct{})
|
|
|
|
watchers[0].receivedFirstProgress = make(chan struct{})
|
2022-01-22 06:40:35 -05:00
|
|
|
xfer, watchers[0].watcher = tm.transfer("id1", makeXferFunc("id1"), progress.ChanOutput(watchers[0].progressChan))
|
2015-11-13 19:59:01 -05:00
|
|
|
go progressConsumer(watchers[0])
|
|
|
|
|
|
|
|
// Give it multiple watchers
|
|
|
|
for i := 1; i != len(watchers); i++ {
|
|
|
|
watchers[i].progressChan = make(chan progress.Progress)
|
|
|
|
watchers[i].progressDone = make(chan struct{})
|
|
|
|
watchers[i].receivedFirstProgress = make(chan struct{})
|
2022-02-18 08:03:35 -05:00
|
|
|
watchers[i].watcher = xfer.watch(progress.ChanOutput(watchers[i].progressChan))
|
2015-11-13 19:59:01 -05:00
|
|
|
go progressConsumer(watchers[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the watchers are set up, allow the transfer goroutine to
|
|
|
|
// proceed.
|
|
|
|
close(ready)
|
|
|
|
|
|
|
|
// Confirm that each watcher gets progress output.
|
|
|
|
for _, w := range watchers {
|
|
|
|
<-w.receivedFirstProgress
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release one watcher every 5ms
|
|
|
|
for _, w := range watchers {
|
2022-02-18 08:03:35 -05:00
|
|
|
xfer.release(w.watcher)
|
2015-11-13 19:59:01 -05:00
|
|
|
<-time.After(5 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that all watchers have been released, Released() should
|
|
|
|
// return a closed channel.
|
2022-02-18 08:03:35 -05:00
|
|
|
<-xfer.released()
|
2015-11-13 19:59:01 -05:00
|
|
|
|
|
|
|
// Done() should return a closed channel because the xfer func returned
|
|
|
|
// due to cancellation.
|
2022-02-18 08:03:35 -05:00
|
|
|
<-xfer.done()
|
2015-11-13 19:59:01 -05:00
|
|
|
|
|
|
|
for _, w := range watchers {
|
|
|
|
close(w.progressChan)
|
|
|
|
<-w.progressDone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 14:37:09 -05:00
|
|
|
func TestWatchFinishedTransfer(t *testing.T) {
|
2022-02-18 10:54:12 -05:00
|
|
|
makeXferFunc := func(id string) doFunc {
|
2022-01-22 07:11:24 -05:00
|
|
|
return func(progressChan chan<- progress.Progress, _ <-chan struct{}, _ chan<- struct{}) transfer {
|
2022-01-22 07:04:46 -05:00
|
|
|
xfer := newTransfer()
|
2016-01-25 14:37:09 -05:00
|
|
|
go func() {
|
|
|
|
// Finish immediately
|
|
|
|
close(progressChan)
|
|
|
|
}()
|
|
|
|
return xfer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 06:19:42 -05:00
|
|
|
tm := newTransferManager(5)
|
2016-01-25 14:37:09 -05:00
|
|
|
|
|
|
|
// Start a transfer
|
2022-01-22 08:32:18 -05:00
|
|
|
watchers := make([]*watcher, 3)
|
2022-01-22 07:11:24 -05:00
|
|
|
var xfer transfer
|
2022-01-22 06:40:35 -05:00
|
|
|
xfer, watchers[0] = tm.transfer("id1", makeXferFunc("id1"), progress.ChanOutput(make(chan progress.Progress)))
|
2016-01-25 14:37:09 -05:00
|
|
|
|
|
|
|
// Give it a watcher immediately
|
2022-02-18 08:03:35 -05:00
|
|
|
watchers[1] = xfer.watch(progress.ChanOutput(make(chan progress.Progress)))
|
2016-01-25 14:37:09 -05:00
|
|
|
|
|
|
|
// Wait for the transfer to complete
|
2022-02-18 08:03:35 -05:00
|
|
|
<-xfer.done()
|
2016-01-25 14:37:09 -05:00
|
|
|
|
|
|
|
// Set up another watcher
|
2022-02-18 08:03:35 -05:00
|
|
|
watchers[2] = xfer.watch(progress.ChanOutput(make(chan progress.Progress)))
|
2016-01-25 14:37:09 -05:00
|
|
|
|
|
|
|
// Release the watchers
|
|
|
|
for _, w := range watchers {
|
2022-02-18 08:03:35 -05:00
|
|
|
xfer.release(w)
|
2016-01-25 14:37:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that all watchers have been released, Released() should
|
|
|
|
// return a closed channel.
|
2022-02-18 08:03:35 -05:00
|
|
|
<-xfer.released()
|
2016-01-25 14:37:09 -05:00
|
|
|
}
|
|
|
|
|
2015-11-13 19:59:01 -05:00
|
|
|
func TestDuplicateTransfer(t *testing.T) {
|
|
|
|
ready := make(chan struct{})
|
|
|
|
|
|
|
|
var xferFuncCalls int32
|
|
|
|
|
2022-02-18 10:54:12 -05:00
|
|
|
makeXferFunc := func(id string) doFunc {
|
2022-01-22 07:11:24 -05:00
|
|
|
return func(progressChan chan<- progress.Progress, _ <-chan struct{}, _ chan<- struct{}) transfer {
|
2015-11-13 19:59:01 -05:00
|
|
|
atomic.AddInt32(&xferFuncCalls, 1)
|
2022-01-22 07:04:46 -05:00
|
|
|
xfer := newTransfer()
|
2015-11-13 19:59:01 -05:00
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
close(progressChan)
|
|
|
|
}()
|
|
|
|
<-ready
|
|
|
|
for i := int64(0); ; i++ {
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
2022-02-18 08:03:35 -05:00
|
|
|
case <-xfer.context().Done():
|
2015-11-13 19:59:01 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
progressChan <- progress.Progress{ID: id, Action: "testing", Current: i, Total: 10}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return xfer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 06:19:42 -05:00
|
|
|
tm := newTransferManager(5)
|
2015-11-13 19:59:01 -05:00
|
|
|
|
|
|
|
type transferInfo struct {
|
2022-01-22 07:11:24 -05:00
|
|
|
xfer transfer
|
2022-01-22 08:32:18 -05:00
|
|
|
watcher *watcher
|
2015-11-13 19:59:01 -05:00
|
|
|
progressChan chan progress.Progress
|
|
|
|
progressDone chan struct{}
|
|
|
|
receivedFirstProgress chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
progressConsumer := func(t transferInfo) {
|
|
|
|
first := true
|
|
|
|
for range t.progressChan {
|
|
|
|
if first {
|
|
|
|
close(t.receivedFirstProgress)
|
|
|
|
}
|
|
|
|
first = false
|
|
|
|
}
|
|
|
|
close(t.progressDone)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to start multiple transfers with the same ID
|
|
|
|
transfers := make([]transferInfo, 5)
|
|
|
|
for i := range transfers {
|
|
|
|
t := &transfers[i]
|
|
|
|
t.progressChan = make(chan progress.Progress)
|
|
|
|
t.progressDone = make(chan struct{})
|
|
|
|
t.receivedFirstProgress = make(chan struct{})
|
2022-01-22 06:40:35 -05:00
|
|
|
t.xfer, t.watcher = tm.transfer("id1", makeXferFunc("id1"), progress.ChanOutput(t.progressChan))
|
2015-11-13 19:59:01 -05:00
|
|
|
go progressConsumer(*t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow the transfer goroutine to proceed.
|
|
|
|
close(ready)
|
|
|
|
|
|
|
|
// Confirm that each watcher gets progress output.
|
|
|
|
for _, t := range transfers {
|
|
|
|
<-t.receivedFirstProgress
|
|
|
|
}
|
|
|
|
|
|
|
|
// Confirm that the transfer function was called exactly once.
|
|
|
|
if xferFuncCalls != 1 {
|
|
|
|
t.Fatal("transfer function wasn't called exactly once")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release one watcher every 5ms
|
|
|
|
for _, t := range transfers {
|
2022-02-18 08:03:35 -05:00
|
|
|
t.xfer.release(t.watcher)
|
2015-11-13 19:59:01 -05:00
|
|
|
<-time.After(5 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range transfers {
|
|
|
|
// Now that all watchers have been released, Released() should
|
|
|
|
// return a closed channel.
|
2022-02-18 08:03:35 -05:00
|
|
|
<-t.xfer.released()
|
2015-11-13 19:59:01 -05:00
|
|
|
// Done() should return a closed channel because the xfer func returned
|
|
|
|
// due to cancellation.
|
2022-02-18 08:03:35 -05:00
|
|
|
<-t.xfer.done()
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range transfers {
|
|
|
|
close(t.progressChan)
|
|
|
|
<-t.progressDone
|
|
|
|
}
|
|
|
|
}
|