Skip to content

Commit 3202b1d

Browse files
committed
add testing for divid and bugfix
1 parent afac708 commit 3202b1d

File tree

4 files changed

+134
-6
lines changed

4 files changed

+134
-6
lines changed

kadai3/imura81gt/rget/go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/gopherdojo/dojo7/kadai3/imura81gt/rget
22

33
go 1.13
44

5-
require golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
5+
require (
6+
github.com/google/go-cmp v0.3.1
7+
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
8+
)

kadai3/imura81gt/rget/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
2+
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
13
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
24
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

kadai3/imura81gt/rget/rget.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,18 @@ type Option struct {
2222
}
2323

2424
type Unit struct {
25-
RangeStart int64
26-
RangeEnd int64
27-
TempFileName string
25+
RangeStart int64
26+
RangeEnd int64
27+
TempFileName string
28+
DownloadedSize int64
29+
}
30+
31+
func (u *Unit) Write(data []byte) (int, error) {
32+
d := len(data)
33+
u.DownloadedSize += int64(d)
34+
// fmt.Printf("%v is downloaded %v/%v \n",
35+
// u.TempFileName, u.DownloadedSize, u.RangeEnd-u.RangeStart+1)
36+
return d, nil
2837
}
2938

3039
type Units []Unit
@@ -105,7 +114,7 @@ func (o *Option) divide() {
105114
}
106115

107116
// TODO: should distribute the remainder to each unit
108-
units[len(units)-1].RangeEnd = o.ContentLength
117+
units[len(units)-1].RangeEnd = (o.ContentLength - 1)
109118

110119
o.Units = units
111120
}
@@ -178,7 +187,7 @@ func (o *Option) downloadWithContext(
178187
return nil
179188
}()
180189

181-
_, err = io.Copy(w, resp.Body)
190+
_, err = io.Copy(w, io.TeeReader(resp.Body, &o.Units[i]))
182191
if err != nil {
183192
return fmt.Errorf("Error: %v", err)
184193
}

kadai3/imura81gt/rget/rget_test.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,115 @@
11
package rget
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
7+
)
8+
9+
func TestWrite(t *testing.T) {
10+
}
11+
12+
func TestRun(t *testing.T) {
13+
}
14+
15+
func TestContentLength(t *testing.T) {
16+
}
17+
18+
func TestAcceptRangesHeaderCheck(t *testing.T) {
19+
}
20+
21+
//func divide(contentLength int64, concurrency int) Units {
22+
func TestDivide(t *testing.T) {
23+
24+
const (
25+
url = "https://example.com/test.iso"
26+
contentLength = 5
27+
)
28+
29+
testCases := []struct {
30+
caseName string
31+
concurrency uint
32+
expected Option
33+
}{
34+
{
35+
caseName: "ContentLength:5/5",
36+
concurrency: 5,
37+
expected: Option{
38+
URL: url,
39+
ContentLength: contentLength,
40+
Concurrency: 5,
41+
Units: []Unit{
42+
{TempFileName: "0_test.iso", RangeStart: 0, RangeEnd: 0},
43+
{TempFileName: "1_test.iso", RangeStart: 1, RangeEnd: 1},
44+
{TempFileName: "2_test.iso", RangeStart: 2, RangeEnd: 2},
45+
{TempFileName: "3_test.iso", RangeStart: 3, RangeEnd: 3},
46+
{TempFileName: "4_test.iso", RangeStart: 4, RangeEnd: 4},
47+
},
48+
},
49+
},
50+
{
51+
caseName: "ContentLength:5/1",
52+
concurrency: 1,
53+
expected: Option{
54+
URL: url,
55+
ContentLength: contentLength,
56+
Concurrency: 1,
57+
Units: []Unit{
58+
{TempFileName: "0_test.iso", RangeStart: 0, RangeEnd: 4},
59+
},
60+
},
61+
},
62+
{
63+
caseName: "ContentLength:5/10",
64+
concurrency: 10,
65+
expected: Option{
66+
URL: url,
67+
ContentLength: contentLength,
68+
Concurrency: 5,
69+
Units: []Unit{
70+
{TempFileName: "0_test.iso", RangeStart: 0, RangeEnd: 0},
71+
{TempFileName: "1_test.iso", RangeStart: 1, RangeEnd: 1},
72+
{TempFileName: "2_test.iso", RangeStart: 2, RangeEnd: 2},
73+
{TempFileName: "3_test.iso", RangeStart: 3, RangeEnd: 3},
74+
{TempFileName: "4_test.iso", RangeStart: 4, RangeEnd: 4},
75+
},
76+
},
77+
},
78+
{
79+
caseName: "ContentLength:5/0",
80+
concurrency: 0,
81+
expected: Option{
82+
URL: url,
83+
ContentLength: contentLength,
84+
Concurrency: 1,
85+
Units: []Unit{
86+
{TempFileName: "0_test.iso", RangeStart: 0, RangeEnd: 4},
87+
},
88+
},
89+
},
90+
}
91+
92+
for _, tc := range testCases {
93+
tc := tc
94+
t.Run(tc.caseName, func(t *testing.T) {
95+
t.Parallel()
96+
97+
o := Option{URL: url, ContentLength: contentLength, Concurrency: tc.concurrency}
98+
o.divide()
99+
100+
if !cmp.Equal(o, tc.expected) {
101+
t.Errorf("actual: %+v\nexpected: %+v\n", o, tc.expected)
102+
}
103+
104+
})
105+
}
106+
}
107+
108+
func TestParallelDownload(t *testing.T) {
109+
}
110+
111+
func TestDownloadWithContext(t *testing.T) {
112+
}
113+
114+
func TestCombine(t *testing.T) {
115+
}

0 commit comments

Comments
 (0)