aboutsummaryrefslogtreecommitdiffhomepage
path: root/cmd/mbf/internal/ci/ci_test.go
blob: 447c4ab1b85f97a8c58b124e3db7253f96da9772 (plain)
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
package ci_test

import (
	"bytes"
	"context"
	"os"
	"path/filepath"
	"testing"

	"hakurei.app/cmd/mbf/internal/ci"
)

func TestFollow(t *testing.T) {
	t.Parallel()

	pathname := filepath.Join(t.TempDir(), "f")
	w, err := os.Create(pathname)
	if err != nil {
		t.Fatal(err)
	}

	var buf bytes.Buffer
	ctx, cancel := context.WithCancel(t.Context())

	var want string
	go func() {
		defer cancel()
		for _, s := range []string{
			"\xde\xad\xbe\xef",
			"\xff\xff\xff\xff",
			"\x00\x00",
		} {
			want += s
			if _, _err := w.WriteString(s); _err != nil {
				panic(_err)
			}
		}
	}()

retry:
	if err = ci.Follow(ctx, pathname, &buf); err != nil {
		t.Fatal(err)
	}
	<-ctx.Done()

	// the inotify event takes time to arrive, and there is no way to
	// synchronise for this cleanly
	if buf.Len() != len(want) {
		buf.Reset()
		goto retry
	}

	if got := buf.String(); got != want {
		t.Fatalf("follow: %q, want %q", got, want)
	}
}