File: display_test.go

package info (click to toggle)
docker.io 28.5.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 69,048 kB
  • sloc: sh: 5,867; makefile: 863; ansic: 184; python: 162; asm: 159
file content (65 lines) | stat: -rw-r--r-- 1,217 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package jsonstream

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"testing"
	"time"

	"github.com/docker/cli/cli/streams"
	"gotest.tools/v3/assert"
)

func TestDisplay(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	t.Cleanup(cancel)

	client, server := io.Pipe()
	t.Cleanup(func() {
		assert.NilError(t, server.Close())
	})

	go func() {
		enc := json.NewEncoder(server)
		for i := 0; i < 100; i++ {
			select {
			case <-ctx.Done():
				assert.NilError(t, server.Close(), "failed to close jsonmessage server")
				return
			default:
				err := enc.Encode(JSONMessage{
					Status: "Downloading",
					ID:     fmt.Sprintf("id-%d", i),
					Progress: &JSONProgress{
						Current: int64(i),
						Total:   100,
						Start:   0,
					},
				})
				if err != nil {
					break
				}
				time.Sleep(100 * time.Millisecond)
			}
		}
	}()

	streamCtx, cancelStream := context.WithCancel(context.Background())
	t.Cleanup(cancelStream)

	done := make(chan error)
	go func() {
		out := streams.NewOut(io.Discard)
		done <- Display(streamCtx, client, out)
	}()

	cancelStream()

	select {
	case <-time.After(time.Second * 3):
	case err := <-done:
		assert.ErrorIs(t, err, context.Canceled)
	}
}