-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
335 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package fecdecoder | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"oneway-filesync/pkg/structs" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/klauspost/reedsolomon" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func createChunks(t *testing.T, required int, total int) []*structs.Chunk { | ||
fec, err := reedsolomon.New(required, total-required) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
shares, err := fec.Split(make([]byte, 400)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Encode the parity set | ||
err = fec.Encode(shares) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
chunks := make([]*structs.Chunk, total) | ||
for i, sharedata := range shares { | ||
chunks[i] = &structs.Chunk{ | ||
ShareIndex: uint32(i), | ||
Data: sharedata, | ||
} | ||
} | ||
return chunks | ||
|
||
} | ||
|
||
func Test_worker(t *testing.T) { | ||
type args struct { | ||
required int | ||
total int | ||
input []*structs.Chunk | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
expectedErr string | ||
}{ | ||
{"test-works", args{2, 4, createChunks(t, 2, 4)}, false, ""}, | ||
{"test-too-few-shards", args{4, 8, createChunks(t, 4, 8)[:3]}, true, "Error FEC decoding shares: too few shards given"}, | ||
{"test-invalid-fec1", args{2, 1, make([]*structs.Chunk, 4)}, true, "Error creating fec object: cannot create Encoder with less than one data shard or less than zero parity shards"}, | ||
{"test-invalid-fec2", args{0, 1, make([]*structs.Chunk, 4)}, true, "Error creating fec object: cannot create Encoder with less than one data shard or less than zero parity shards"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var memLog bytes.Buffer | ||
logrus.SetOutput(&memLog) | ||
|
||
input := make(chan []*structs.Chunk, 5) | ||
output := make(chan *structs.Chunk, 5) | ||
|
||
input <- tt.args.input | ||
|
||
conf := fecDecoderConfig{tt.args.required, tt.args.total, input, output} | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
go func() { | ||
time.Sleep(2 * time.Second) | ||
cancel() | ||
}() | ||
// ch <- tt.args.file | ||
worker(ctx, &conf) | ||
|
||
if tt.wantErr { | ||
if !strings.Contains(memLog.String(), tt.expectedErr) { | ||
t.Fatalf("Expected not in log, '%v' not in '%v'", tt.expectedErr, memLog.String()) | ||
} | ||
} else { | ||
<-output | ||
} | ||
|
||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package fecencoder | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"oneway-filesync/pkg/structs" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func Test_worker(t *testing.T) { | ||
type args struct { | ||
required int | ||
total int | ||
input *structs.Chunk | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
expectedErr string | ||
}{ | ||
{"test-works", args{2, 4, &structs.Chunk{Data: make([]byte, 400)}}, false, ""}, | ||
{"test-shortdata1", args{2, 4, &structs.Chunk{Data: make([]byte, 0)}}, true, "Error splitting chunk: not enough data to fill the number of requested shards"}, | ||
{"test-invalid-fec1", args{2, 1, &structs.Chunk{}}, true, "Error creating fec object: cannot create Encoder with less than one data shard or less than zero parity shards"}, | ||
{"test-invalid-fec2", args{0, 1, &structs.Chunk{}}, true, "Error creating fec object: cannot create Encoder with less than one data shard or less than zero parity shards"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var memLog bytes.Buffer | ||
logrus.SetOutput(&memLog) | ||
|
||
input := make(chan *structs.Chunk, 5) | ||
output := make(chan *structs.Chunk, 5) | ||
|
||
input <- tt.args.input | ||
|
||
conf := fecEncoderConfig{tt.args.required, tt.args.total, input, output} | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
go func() { | ||
time.Sleep(2 * time.Second) | ||
cancel() | ||
}() | ||
// ch <- tt.args.file | ||
worker(ctx, &conf) | ||
|
||
if tt.wantErr { | ||
if !strings.Contains(memLog.String(), tt.expectedErr) { | ||
t.Fatalf("Expected not in log, '%v' not in '%v'", tt.expectedErr, memLog.String()) | ||
} | ||
} else { | ||
for i := 0; i < conf.total; i++ { | ||
<-output | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package udpsender | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/rand" | ||
"math/big" | ||
"net" | ||
"oneway-filesync/pkg/structs" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func randint(max int64) int { | ||
nBig, err := rand.Int(rand.Reader, big.NewInt(max)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return int(nBig.Int64()) | ||
} | ||
|
||
func Test_worker(t *testing.T) { | ||
ip := "127.0.0.1" | ||
port := randint(30000) + 30000 | ||
addr := net.UDPAddr{ | ||
IP: net.ParseIP(ip), | ||
Port: port, | ||
} | ||
|
||
receiving_conn, err := net.ListenUDP("udp", &addr) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer receiving_conn.Close() | ||
|
||
type args struct { | ||
ip string | ||
port int | ||
chunk structs.Chunk | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
expectedErr string | ||
}{ | ||
{"test-works", args{ip, port, structs.Chunk{}}, false, ""}, | ||
{"test-socket-err", args{ip, 88888, structs.Chunk{}}, true, "Error creating udp socket"}, | ||
{"test-message-too-long", args{ip, port, structs.Chunk{Data: make([]byte, 100*1024)}}, true, "Error sending share: write udp"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var memLog bytes.Buffer | ||
logrus.SetOutput(&memLog) | ||
|
||
input := make(chan *structs.Chunk, 5) | ||
input <- &tt.args.chunk | ||
conf := udpSenderConfig{tt.args.ip, tt.args.port, input} | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
go func() { | ||
time.Sleep(2 * time.Second) | ||
cancel() | ||
}() | ||
worker(ctx, &conf) | ||
|
||
if tt.wantErr { | ||
if !strings.Contains(memLog.String(), tt.expectedErr) { | ||
t.Fatalf("Expected not in log, '%v' not in '%v'", tt.expectedErr, memLog.String()) | ||
} | ||
} else { | ||
_, _ = receiving_conn.Read(make([]byte, 8192)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.