@@ -3,7 +3,6 @@ package main
33import (
44 "bytes"
55 "crypto/md5"
6- crand "crypto/rand"
76 "fmt"
87 "io"
98 "io/ioutil"
@@ -16,7 +15,9 @@ import (
1615 "github.com/ethereum/go-ethereum/common/hexutil"
1716 "github.com/ethereum/go-ethereum/crypto"
1817 "github.com/ethereum/go-ethereum/log"
18+ "github.com/ethereum/go-ethereum/metrics"
1919 "github.com/ethereum/go-ethereum/swarm/storage/feed"
20+ "github.com/ethereum/go-ethereum/swarm/testutil"
2021 "github.com/pborman/uuid"
2122 cli "gopkg.in/urfave/cli.v1"
2223)
@@ -25,13 +26,28 @@ const (
2526 feedRandomDataLength = 8
2627)
2728
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 )
3131
32- generateEndpoints (scheme , cluster , appName , from , to )
32+ go func () {
33+ errc <- feedUploadAndSync (ctx , tuid )
34+ }()
3335
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" )
3551
3652 // create a random private key to sign updates with and derive the address
3753 pkFile , err := ioutil .TempFile ("" , "swarm-feed-smoke-test" )
@@ -85,7 +101,7 @@ func feedUploadAndSync(c *cli.Context) error {
85101
86102 // create feed manifest, topic only
87103 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 )
89105 cmd .Stdout = & out
90106 log .Debug ("create feed manifest topic cmd" , "cmd" , cmd )
91107 err = cmd .Run ()
@@ -100,7 +116,7 @@ func feedUploadAndSync(c *cli.Context) error {
100116 out .Reset ()
101117
102118 // 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 )
104120 cmd .Stdout = & out
105121 log .Debug ("create feed manifest subtopic cmd" , "cmd" , cmd )
106122 err = cmd .Run ()
@@ -115,7 +131,7 @@ func feedUploadAndSync(c *cli.Context) error {
115131 out .Reset ()
116132
117133 // 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 )
119135 cmd .Stdout = & out
120136 log .Debug ("create feed manifest mergetopic cmd" , "cmd" , cmd )
121137 err = cmd .Run ()
@@ -141,7 +157,7 @@ func feedUploadAndSync(c *cli.Context) error {
141157 dataHex := hexutil .Encode (data )
142158
143159 // 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 )
145161 cmd .Stdout = & out
146162 log .Debug ("update feed manifest topic cmd" , "cmd" , cmd )
147163 err = cmd .Run ()
@@ -152,7 +168,7 @@ func feedUploadAndSync(c *cli.Context) error {
152168 out .Reset ()
153169
154170 // 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 )
156172 cmd .Stdout = & out
157173 log .Debug ("update feed manifest subtopic cmd" , "cmd" , cmd )
158174 err = cmd .Run ()
@@ -163,7 +179,7 @@ func feedUploadAndSync(c *cli.Context) error {
163179 out .Reset ()
164180
165181 // 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 )
167183 cmd .Stdout = & out
168184 log .Debug ("update feed manifest merged topic cmd" , "cmd" , cmd )
169185 err = cmd .Run ()
@@ -177,36 +193,33 @@ func feedUploadAndSync(c *cli.Context) error {
177193
178194 // retrieve the data
179195 wg := sync.WaitGroup {}
180- for _ , endpoint := range endpoints {
196+ for _ , host := range hosts {
181197 // raw retrieve, topic only
182198 for _ , hex := range []string {topicHex , subTopicOnlyHex , mergedSubTopicHex } {
183199 wg .Add (1 )
184200 ruid := uuid .New ()[:8 ]
185201 go func (hex string , endpoint string , ruid string ) {
186202 for {
187- err := fetchFeed (hex , userHex , endpoint , dataHash , ruid )
203+ err := fetchFeed (hex , userHex , httpEndpoint ( host ) , dataHash , ruid )
188204 if err != nil {
189205 continue
190206 }
191207
192208 wg .Done ()
193209 return
194210 }
195- }(hex , endpoint , ruid )
196-
211+ }(hex , httpEndpoint (host ), ruid )
197212 }
198213 }
199214 wg .Wait ()
200215 log .Info ("all endpoints synced random data successfully" )
201216
202217 // 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 )
205219
206- h = md5 .New ()
207- r := io .TeeReader (io .LimitReader (crand .Reader , int64 (filesize * 1000 )), h )
220+ randomBytes := testutil .RandomBytes (seed , filesize * 1000 )
208221
209- hash , err := upload (r , filesize * 1000 , endpoints [0 ])
222+ hash , err := upload (randomBytes , httpEndpoint ( hosts [0 ]) )
210223 if err != nil {
211224 return err
212225 }
@@ -220,7 +233,7 @@ func feedUploadAndSync(c *cli.Context) error {
220233 log .Info ("uploaded successfully" , "hash" , hash , "digest" , fmt .Sprintf ("%x" , fileHash ))
221234
222235 // 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 )
224237 cmd .Stdout = & out
225238 err = cmd .Run ()
226239 if err != nil {
@@ -230,7 +243,7 @@ func feedUploadAndSync(c *cli.Context) error {
230243 out .Reset ()
231244
232245 // 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 )
234247 cmd .Stdout = & out
235248 err = cmd .Run ()
236249 if err != nil {
@@ -240,7 +253,7 @@ func feedUploadAndSync(c *cli.Context) error {
240253 out .Reset ()
241254
242255 // 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 )
244257 cmd .Stdout = & out
245258 err = cmd .Run ()
246259 if err != nil {
@@ -251,23 +264,23 @@ func feedUploadAndSync(c *cli.Context) error {
251264
252265 time .Sleep (3 * time .Second )
253266
254- for _ , endpoint := range endpoints {
267+ for _ , host := range hosts {
255268
256269 // manifest retrieve, topic only
257270 for _ , url := range []string {manifestWithTopic , manifestWithSubTopic , manifestWithMergedTopic } {
258271 wg .Add (1 )
259272 ruid := uuid .New ()[:8 ]
260273 go func (url string , endpoint string , ruid string ) {
261274 for {
262- err := fetch (url , endpoint , fileHash , ruid )
275+ err := fetch (url , endpoint , fileHash , ruid , "" )
263276 if err != nil {
264277 continue
265278 }
266279
267280 wg .Done ()
268281 return
269282 }
270- }(url , endpoint , ruid )
283+ }(url , httpEndpoint ( host ) , ruid )
271284 }
272285
273286 }
0 commit comments