@@ -3,7 +3,6 @@ package main
3
3
import (
4
4
"bytes"
5
5
"crypto/md5"
6
- crand "crypto/rand"
7
6
"fmt"
8
7
"io"
9
8
"io/ioutil"
@@ -16,7 +15,9 @@ import (
16
15
"github.com/ethereum/go-ethereum/common/hexutil"
17
16
"github.com/ethereum/go-ethereum/crypto"
18
17
"github.com/ethereum/go-ethereum/log"
18
+ "github.com/ethereum/go-ethereum/metrics"
19
19
"github.com/ethereum/go-ethereum/swarm/storage/feed"
20
+ "github.com/ethereum/go-ethereum/swarm/testutil"
20
21
"github.com/pborman/uuid"
21
22
cli "gopkg.in/urfave/cli.v1"
22
23
)
@@ -25,13 +26,28 @@ const (
25
26
feedRandomDataLength = 8
26
27
)
27
28
28
- // TODO: retrieve with manifest + extract repeating code
29
- func feedUploadAndSync (c * cli.Context ) error {
30
- defer func (now time.Time ) { log .Info ("total time" , "time" , time .Since (now ), "size (kb)" , filesize ) }(time .Now ())
29
+ func feedUploadAndSyncCmd (ctx * cli.Context , tuid string ) error {
30
+ errc := make (chan error )
31
31
32
- generateEndpoints (scheme , cluster , appName , from , to )
32
+ go func () {
33
+ errc <- feedUploadAndSync (ctx , tuid )
34
+ }()
33
35
34
- log .Info ("generating and uploading feeds to " + endpoints [0 ] + " and syncing" )
36
+ select {
37
+ case err := <- errc :
38
+ if err != nil {
39
+ metrics .GetOrRegisterCounter (fmt .Sprintf ("%s.fail" , commandName ), nil ).Inc (1 )
40
+ }
41
+ return err
42
+ case <- time .After (time .Duration (timeout ) * time .Second ):
43
+ metrics .GetOrRegisterCounter (fmt .Sprintf ("%s.timeout" , commandName ), nil ).Inc (1 )
44
+
45
+ return fmt .Errorf ("timeout after %v sec" , timeout )
46
+ }
47
+ }
48
+
49
+ func feedUploadAndSync (c * cli.Context , tuid string ) error {
50
+ log .Info ("generating and uploading feeds to " + httpEndpoint (hosts [0 ]) + " and syncing" )
35
51
36
52
// create a random private key to sign updates with and derive the address
37
53
pkFile , err := ioutil .TempFile ("" , "swarm-feed-smoke-test" )
@@ -85,7 +101,7 @@ func feedUploadAndSync(c *cli.Context) error {
85
101
86
102
// create feed manifest, topic only
87
103
var out bytes.Buffer
88
- cmd := exec .Command ("swarm" , "--bzzapi" , endpoints [0 ], "feed" , "create" , "--topic" , topicHex , "--user" , userHex )
104
+ cmd := exec .Command ("swarm" , "--bzzapi" , httpEndpoint ( hosts [0 ]) , "feed" , "create" , "--topic" , topicHex , "--user" , userHex )
89
105
cmd .Stdout = & out
90
106
log .Debug ("create feed manifest topic cmd" , "cmd" , cmd )
91
107
err = cmd .Run ()
@@ -100,7 +116,7 @@ func feedUploadAndSync(c *cli.Context) error {
100
116
out .Reset ()
101
117
102
118
// create feed manifest, subtopic only
103
- cmd = exec .Command ("swarm" , "--bzzapi" , endpoints [0 ], "feed" , "create" , "--name" , subTopicHex , "--user" , userHex )
119
+ cmd = exec .Command ("swarm" , "--bzzapi" , httpEndpoint ( hosts [0 ]) , "feed" , "create" , "--name" , subTopicHex , "--user" , userHex )
104
120
cmd .Stdout = & out
105
121
log .Debug ("create feed manifest subtopic cmd" , "cmd" , cmd )
106
122
err = cmd .Run ()
@@ -115,7 +131,7 @@ func feedUploadAndSync(c *cli.Context) error {
115
131
out .Reset ()
116
132
117
133
// create feed manifest, merged topic
118
- cmd = exec .Command ("swarm" , "--bzzapi" , endpoints [0 ], "feed" , "create" , "--topic" , topicHex , "--name" , subTopicHex , "--user" , userHex )
134
+ cmd = exec .Command ("swarm" , "--bzzapi" , httpEndpoint ( hosts [0 ]) , "feed" , "create" , "--topic" , topicHex , "--name" , subTopicHex , "--user" , userHex )
119
135
cmd .Stdout = & out
120
136
log .Debug ("create feed manifest mergetopic cmd" , "cmd" , cmd )
121
137
err = cmd .Run ()
@@ -141,7 +157,7 @@ func feedUploadAndSync(c *cli.Context) error {
141
157
dataHex := hexutil .Encode (data )
142
158
143
159
// update with topic
144
- cmd = exec .Command ("swarm" , "--bzzaccount" , pkFile .Name (), "--bzzapi" , endpoints [0 ], "feed" , "update" , "--topic" , topicHex , dataHex )
160
+ cmd = exec .Command ("swarm" , "--bzzaccount" , pkFile .Name (), "--bzzapi" , httpEndpoint ( hosts [0 ]) , "feed" , "update" , "--topic" , topicHex , dataHex )
145
161
cmd .Stdout = & out
146
162
log .Debug ("update feed manifest topic cmd" , "cmd" , cmd )
147
163
err = cmd .Run ()
@@ -152,7 +168,7 @@ func feedUploadAndSync(c *cli.Context) error {
152
168
out .Reset ()
153
169
154
170
// update with subtopic
155
- cmd = exec .Command ("swarm" , "--bzzaccount" , pkFile .Name (), "--bzzapi" , endpoints [0 ], "feed" , "update" , "--name" , subTopicHex , dataHex )
171
+ cmd = exec .Command ("swarm" , "--bzzaccount" , pkFile .Name (), "--bzzapi" , httpEndpoint ( hosts [0 ]) , "feed" , "update" , "--name" , subTopicHex , dataHex )
156
172
cmd .Stdout = & out
157
173
log .Debug ("update feed manifest subtopic cmd" , "cmd" , cmd )
158
174
err = cmd .Run ()
@@ -163,7 +179,7 @@ func feedUploadAndSync(c *cli.Context) error {
163
179
out .Reset ()
164
180
165
181
// update with merged topic
166
- cmd = exec .Command ("swarm" , "--bzzaccount" , pkFile .Name (), "--bzzapi" , endpoints [0 ], "feed" , "update" , "--topic" , topicHex , "--name" , subTopicHex , dataHex )
182
+ cmd = exec .Command ("swarm" , "--bzzaccount" , pkFile .Name (), "--bzzapi" , httpEndpoint ( hosts [0 ]) , "feed" , "update" , "--topic" , topicHex , "--name" , subTopicHex , dataHex )
167
183
cmd .Stdout = & out
168
184
log .Debug ("update feed manifest merged topic cmd" , "cmd" , cmd )
169
185
err = cmd .Run ()
@@ -177,36 +193,33 @@ func feedUploadAndSync(c *cli.Context) error {
177
193
178
194
// retrieve the data
179
195
wg := sync.WaitGroup {}
180
- for _ , endpoint := range endpoints {
196
+ for _ , host := range hosts {
181
197
// raw retrieve, topic only
182
198
for _ , hex := range []string {topicHex , subTopicOnlyHex , mergedSubTopicHex } {
183
199
wg .Add (1 )
184
200
ruid := uuid .New ()[:8 ]
185
201
go func (hex string , endpoint string , ruid string ) {
186
202
for {
187
- err := fetchFeed (hex , userHex , endpoint , dataHash , ruid )
203
+ err := fetchFeed (hex , userHex , httpEndpoint ( host ) , dataHash , ruid )
188
204
if err != nil {
189
205
continue
190
206
}
191
207
192
208
wg .Done ()
193
209
return
194
210
}
195
- }(hex , endpoint , ruid )
196
-
211
+ }(hex , httpEndpoint (host ), ruid )
197
212
}
198
213
}
199
214
wg .Wait ()
200
215
log .Info ("all endpoints synced random data successfully" )
201
216
202
217
// upload test file
203
- seed := int (time .Now ().UnixNano () / 1e6 )
204
- log .Info ("feed uploading to " + endpoints [0 ]+ " and syncing" , "seed" , seed )
218
+ log .Info ("feed uploading to " + httpEndpoint (hosts [0 ])+ " and syncing" , "seed" , seed )
205
219
206
- h = md5 .New ()
207
- r := io .TeeReader (io .LimitReader (crand .Reader , int64 (filesize * 1000 )), h )
220
+ randomBytes := testutil .RandomBytes (seed , filesize * 1000 )
208
221
209
- hash , err := upload (r , filesize * 1000 , endpoints [0 ])
222
+ hash , err := upload (randomBytes , httpEndpoint ( hosts [0 ]) )
210
223
if err != nil {
211
224
return err
212
225
}
@@ -220,7 +233,7 @@ func feedUploadAndSync(c *cli.Context) error {
220
233
log .Info ("uploaded successfully" , "hash" , hash , "digest" , fmt .Sprintf ("%x" , fileHash ))
221
234
222
235
// update file with topic
223
- cmd = exec .Command ("swarm" , "--bzzaccount" , pkFile .Name (), "--bzzapi" , endpoints [0 ], "feed" , "update" , "--topic" , topicHex , multihashHex )
236
+ cmd = exec .Command ("swarm" , "--bzzaccount" , pkFile .Name (), "--bzzapi" , httpEndpoint ( hosts [0 ]) , "feed" , "update" , "--topic" , topicHex , multihashHex )
224
237
cmd .Stdout = & out
225
238
err = cmd .Run ()
226
239
if err != nil {
@@ -230,7 +243,7 @@ func feedUploadAndSync(c *cli.Context) error {
230
243
out .Reset ()
231
244
232
245
// update file with subtopic
233
- cmd = exec .Command ("swarm" , "--bzzaccount" , pkFile .Name (), "--bzzapi" , endpoints [0 ], "feed" , "update" , "--name" , subTopicHex , multihashHex )
246
+ cmd = exec .Command ("swarm" , "--bzzaccount" , pkFile .Name (), "--bzzapi" , httpEndpoint ( hosts [0 ]) , "feed" , "update" , "--name" , subTopicHex , multihashHex )
234
247
cmd .Stdout = & out
235
248
err = cmd .Run ()
236
249
if err != nil {
@@ -240,7 +253,7 @@ func feedUploadAndSync(c *cli.Context) error {
240
253
out .Reset ()
241
254
242
255
// update file with merged topic
243
- cmd = exec .Command ("swarm" , "--bzzaccount" , pkFile .Name (), "--bzzapi" , endpoints [0 ], "feed" , "update" , "--topic" , topicHex , "--name" , subTopicHex , multihashHex )
256
+ cmd = exec .Command ("swarm" , "--bzzaccount" , pkFile .Name (), "--bzzapi" , httpEndpoint ( hosts [0 ]) , "feed" , "update" , "--topic" , topicHex , "--name" , subTopicHex , multihashHex )
244
257
cmd .Stdout = & out
245
258
err = cmd .Run ()
246
259
if err != nil {
@@ -251,23 +264,23 @@ func feedUploadAndSync(c *cli.Context) error {
251
264
252
265
time .Sleep (3 * time .Second )
253
266
254
- for _ , endpoint := range endpoints {
267
+ for _ , host := range hosts {
255
268
256
269
// manifest retrieve, topic only
257
270
for _ , url := range []string {manifestWithTopic , manifestWithSubTopic , manifestWithMergedTopic } {
258
271
wg .Add (1 )
259
272
ruid := uuid .New ()[:8 ]
260
273
go func (url string , endpoint string , ruid string ) {
261
274
for {
262
- err := fetch (url , endpoint , fileHash , ruid )
275
+ err := fetch (url , endpoint , fileHash , ruid , "" )
263
276
if err != nil {
264
277
continue
265
278
}
266
279
267
280
wg .Done ()
268
281
return
269
282
}
270
- }(url , endpoint , ruid )
283
+ }(url , httpEndpoint ( host ) , ruid )
271
284
}
272
285
273
286
}
0 commit comments