-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathbatch_cat_file_test.go
149 lines (122 loc) · 3.43 KB
/
batch_cat_file_test.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package git
import (
"context"
"io"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func Test_GitBatchOperatorsNormal(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
batch, err := NewBatchCatFile(t.Context(), bareRepo1Path, false)
assert.NoError(t, err)
assert.NotNil(t, batch)
defer batch.Close()
err = batch.Input("refs/heads/master")
assert.NoError(t, err)
rd := batch.Reader()
assert.NotNil(t, rd)
_, typ, size, err := ReadBatchLine(rd)
assert.NoError(t, err)
assert.Equal(t, "commit", typ)
assert.Equal(t, int64(1075), size)
// this step is very important, otherwise the next read will be wrong
s, err := rd.Discard(int(size))
assert.NoError(t, err)
assert.EqualValues(t, size, s)
err = batch.Input("ce064814f4a0d337b333e646ece456cd39fab612")
assert.NoError(t, err)
assert.NotNil(t, rd)
_, typ, size, err = ReadBatchLine(rd)
assert.NoError(t, err)
assert.Equal(t, "commit", typ)
assert.Equal(t, int64(1075), size)
s, err = rd.Discard(int(size))
assert.NoError(t, err)
assert.EqualValues(t, size, s)
kases := []struct {
refname string
size int64
}{
{"refs/heads/master", 1075},
{"feaf4ba6bc635fec442f46ddd4512416ec43c2c2", 1074},
{"37991dec2c8e592043f47155ce4808d4580f9123", 239},
}
var inputs []string
for _, kase := range kases {
inputs = append(inputs, kase.refname)
}
// input once for 3 refs
err = batch.Input(inputs...)
assert.NoError(t, err)
assert.NotNil(t, rd)
for i := 0; i < 3; i++ {
_, typ, size, err = ReadBatchLine(rd)
assert.NoError(t, err)
assert.Equal(t, "commit", typ)
assert.Equal(t, kases[i].size, size)
s, err := rd.Discard(int(size))
assert.NoError(t, err)
assert.EqualValues(t, size, s)
}
// input 3 times
for _, input := range inputs {
err = batch.Input(input)
assert.NoError(t, err)
assert.NotNil(t, rd)
}
for i := 0; i < 3; i++ {
_, typ, size, err = ReadBatchLine(rd)
assert.NoError(t, err)
assert.Equal(t, "commit", typ)
assert.Equal(t, kases[i].size, size)
s, err := rd.Discard(int(size))
assert.NoError(t, err)
assert.EqualValues(t, size, s)
}
}
func Test_GitBatchOperatorsCancel(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
batch, err := NewBatchCatFile(t.Context(), bareRepo1Path, false)
assert.NoError(t, err)
assert.NotNil(t, batch)
defer batch.Close()
err = batch.Input("refs/heads/master")
assert.NoError(t, err)
rd := batch.Reader()
assert.NotNil(t, rd)
_, typ, size, err := ReadBatchLine(rd)
assert.NoError(t, err)
assert.Equal(t, "commit", typ)
assert.Equal(t, int64(1075), size)
go func() {
time.Sleep(time.Second)
batch.Cancel()
}()
// block here to wait cancel
_, err = io.ReadAll(rd)
assert.NoError(t, err)
}
func Test_GitBatchOperatorsTimeout(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
ctx, cancel := context.WithTimeout(t.Context(), 1*time.Second)
defer cancel()
batch, err := NewBatchCatFile(ctx, bareRepo1Path, false)
assert.NoError(t, err)
assert.NotNil(t, batch)
defer batch.Close()
err = batch.Input("refs/heads/master")
assert.NoError(t, err)
rd := batch.Reader()
assert.NotNil(t, rd)
_, typ, size, err := ReadBatchLine(rd)
assert.NoError(t, err)
assert.Equal(t, "commit", typ)
assert.Equal(t, int64(1075), size)
// block here until timeout
_, err = io.ReadAll(rd)
assert.NoError(t, err)
}