5
5
"crypto/rand"
6
6
"os"
7
7
"testing"
8
+ "sync"
8
9
9
10
"github.com/stretchr/testify/assert"
10
11
@@ -251,11 +252,20 @@ func TestSyncAnyFile(t *testing.T) {
251
252
252
253
func testSyncAnyFile (t * testing.T , src , dst string , directIO , fastSync bool ) {
253
254
// Sync
255
+ var wg sync.WaitGroup
256
+ wg .Add (1 )
254
257
go func () {
255
- _ = rest .TestServer (context .Background (), port , dst , timeout )
258
+ defer wg .Done ()
259
+
260
+ err := rest .TestServer (context .Background (), port , dst , timeout )
261
+ // http server is closed by the client after file transfer is done, so the error
262
+ // "http: Server closed" is expected.
263
+ assert .True (t , err == nil || err .Error () == "http: Server closed" , "Unexpected error: %v" , err )
256
264
}()
257
265
err := SyncFile (src , localhost + ":" + port , timeout , directIO , fastSync )
258
266
267
+ wg .Wait ()
268
+
259
269
// Verify
260
270
if err != nil {
261
271
t .Fatal ("sync error" )
@@ -271,12 +281,20 @@ func testSyncAnyFile(t *testing.T, src, dst string, directIO, fastSync bool) {
271
281
272
282
func testSyncAnyFileExpectFailure (t * testing.T , src , dst string , directIO , fastSync bool ) {
273
283
// Sync
284
+ var wg sync.WaitGroup
285
+ wg .Add (1 )
274
286
go func () {
287
+ defer wg .Done ()
288
+
275
289
err := rest .TestServer (context .Background (), port , dst , timeout )
276
- assert .Nil (t , err )
290
+ // http server is closed by the client after file transfer is done, so the error
291
+ // "http: Server closed" is expected.
292
+ assert .True (t , err == nil || err .Error () == "http: Server closed" , "Unexpected error: %v" , err )
277
293
}()
278
294
err := SyncFile (src , localhost + ":" + port , timeout , directIO , fastSync )
279
295
296
+ wg .Wait ()
297
+
280
298
// Verify
281
299
if err == nil {
282
300
t .Fatal ("sync error" )
@@ -733,12 +751,20 @@ func testSyncFile(t *testing.T, layoutLocal, layoutRemote []FileInterval, direct
733
751
}
734
752
735
753
// Sync
754
+ var wg sync.WaitGroup
755
+ wg .Add (1 )
736
756
go func () {
757
+ defer wg .Done ()
758
+
737
759
err := rest .TestServer (context .Background (), port , remotePath , timeout )
738
- assert .Nil (t , err )
760
+ // http server is closed by the client after file transfer is done, so the error
761
+ // "http: Server closed" is expected.
762
+ assert .True (t , err == nil || err .Error () == "http: Server closed" , "Unexpected error: %v" , err )
739
763
}()
740
764
err := SyncFile (localPath , localhost + ":" + port , timeout , true /* directIO */ , false /* fastSync */ )
741
765
766
+ wg .Wait ()
767
+
742
768
// Verify
743
769
if err != nil {
744
770
t .Fatal ("sync error" )
@@ -775,37 +801,61 @@ func Benchmark_1G_InitFiles(b *testing.B) {
775
801
}
776
802
777
803
func Benchmark_1G_SendFiles_Whole (b * testing.B ) {
804
+ var wg sync.WaitGroup
805
+ wg .Add (1 )
806
+
778
807
go func () {
808
+ defer wg .Done ()
809
+
779
810
err := rest .TestServer (context .Background (), port , remoteBigPath , timeout )
780
- assert .Nil (b , err )
811
+ // http server is closed by the client after file transfer is done, so the error
812
+ // "http: Server closed" is expected.
813
+ assert .True (b , err == nil || err .Error () == "http: Server closed" , "Unexpected error: %v" , err )
781
814
}()
782
815
err := SyncFile (localBigPath , localhost + ":" + port , timeout , true /* directIO */ , false /* fastSync */ )
783
816
817
+ wg .Wait ()
818
+
784
819
if err != nil {
785
820
b .Fatal ("sync error" )
786
821
}
787
822
}
788
823
789
824
func Benchmark_1G_SendFiles_Whole_No_DirectIO (b * testing.B ) {
825
+ var wg sync.WaitGroup
826
+ wg .Add (1 )
790
827
go func () {
828
+ defer wg .Done ()
829
+
791
830
err := rest .TestServer (context .Background (), port , remoteBigPath , timeout )
792
- assert .Nil (b , err )
831
+ // http server is closed by the client after file transfer is done, so the error
832
+ // "http: Server closed" is expected.
833
+ assert .True (b , err == nil || err .Error () == "http: Server closed" , "Unexpected error: %v" , err )
793
834
}()
794
835
err := SyncFile (localBigPath , localhost + ":" + port , timeout , false /* directIO */ , false /* fastSync */ )
795
836
837
+ wg .Wait ()
838
+
796
839
if err != nil {
797
840
b .Fatal ("sync error" )
798
841
}
799
842
}
800
843
801
844
func Benchmark_1G_SendFiles_Diff (b * testing.B ) {
802
-
845
+ var wg sync.WaitGroup
846
+ wg .Add (1 )
803
847
go func () {
848
+ defer wg .Done ()
849
+
804
850
err := rest .TestServer (context .Background (), port , remoteBigPath , timeout )
805
- assert .Nil (b , err )
851
+ // http server is closed by the client after file transfer is done, so the error
852
+ // "http: Server closed" is expected.
853
+ assert .True (b , err == nil || err .Error () == "http: Server closed" , "Unexpected error: %v" , err )
806
854
}()
807
855
err := SyncFile (localBigPath , localhost + ":" + port , timeout , true /* directIO */ , false /* fastSync */ )
808
856
857
+ wg .Wait ()
858
+
809
859
if err != nil {
810
860
b .Fatal ("sync error" )
811
861
}
@@ -867,12 +917,20 @@ func TestSyncSnapshotZeroByte(t *testing.T) {
867
917
868
918
func testSyncAnyContent (t * testing.T , snapshotName string , dstFileName string , rw ReaderWriterAt , snapshotSize int64 ) {
869
919
// Sync
920
+ var wg sync.WaitGroup
921
+ wg .Add (1 )
870
922
go func () {
923
+ defer wg .Done ()
924
+
871
925
err := rest .TestServer (context .Background (), port , dstFileName , timeout )
872
- assert .Nil (t , err )
926
+ // http server is closed by the client after file transfer is done, so the error
927
+ // "http: Server closed" is expected.
928
+ assert .True (t , err == nil || err .Error () == "http: Server closed" , "Unexpected error: %v" , err )
873
929
}()
874
930
err := SyncContent (snapshotName , rw , snapshotSize , localhost + ":" + port , timeout , true , false )
875
931
932
+ wg .Wait ()
933
+
876
934
// Verify
877
935
if err != nil {
878
936
t .Fatalf ("sync error: %v" , err )
0 commit comments