-
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
1 parent
360db5c
commit 42c91a0
Showing
8 changed files
with
231 additions
and
205 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
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,74 @@ | ||
package split | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/ohkinozomu/fuyuu-router/pkg/data" | ||
) | ||
|
||
func TestNewMerger(t *testing.T) { | ||
merger := NewMerger() | ||
if merger == nil { | ||
t.Error("NewMerger returned nil") | ||
} | ||
if merger.chunks == nil { | ||
t.Error("NewMerger did not initialize chunks map") | ||
} | ||
} | ||
|
||
func TestAddChunkAndIsComplete(t *testing.T) { | ||
merger := NewMerger() | ||
chunk := &data.HTTPBodyChunk{ | ||
RequestId: "test", | ||
Sequence: 1, | ||
Total: 2, | ||
Data: []byte("part1"), | ||
} | ||
|
||
merger.AddChunk(chunk) | ||
if !reflect.DeepEqual(merger.chunks[chunk.RequestId][int(chunk.Sequence)], chunk.Data) { | ||
t.Errorf("AddChunk did not add the chunk data correctly") | ||
} | ||
|
||
if merger.IsComplete(chunk) { | ||
t.Error("IsComplete should return false when the total number of chunks has not been reached") | ||
} | ||
|
||
chunk2 := &data.HTTPBodyChunk{ | ||
RequestId: "test", | ||
Sequence: 2, | ||
Total: 2, | ||
Data: []byte("part2"), | ||
} | ||
|
||
merger.AddChunk(chunk2) | ||
if !merger.IsComplete(chunk2) { | ||
t.Error("IsComplete should return true when all chunks have been added") | ||
} | ||
} | ||
|
||
func TestGetCombinedData(t *testing.T) { | ||
merger := NewMerger() | ||
chunk1 := &data.HTTPBodyChunk{ | ||
RequestId: "test", | ||
Sequence: 1, | ||
Total: 2, | ||
Data: []byte("part1"), | ||
} | ||
chunk2 := &data.HTTPBodyChunk{ | ||
RequestId: "test", | ||
Sequence: 2, | ||
Total: 2, | ||
Data: []byte("part2"), | ||
} | ||
|
||
merger.AddChunk(chunk1) | ||
merger.AddChunk(chunk2) | ||
|
||
combined := merger.GetCombinedData(chunk1) | ||
expected := []byte("part1part2") | ||
if !reflect.DeepEqual(combined, expected) { | ||
t.Errorf("GetCombinedData returned %v, expected %v", combined, expected) | ||
} | ||
} |
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,110 @@ | ||
package split | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSplitChunk(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
body []byte | ||
chunkByte int | ||
want [][]byte | ||
}{ | ||
{ | ||
name: "Empty input", | ||
body: []byte(""), | ||
chunkByte: 4, | ||
want: nil, | ||
}, | ||
{ | ||
name: "Regular input, complete division", | ||
body: []byte("abcdef"), | ||
chunkByte: 2, | ||
want: [][]byte{[]byte("ab"), []byte("cd"), []byte("ef")}, | ||
}, | ||
{ | ||
name: "Regular input, incomplete division", | ||
body: []byte("abcdefgh"), | ||
chunkByte: 3, | ||
want: [][]byte{[]byte("abc"), []byte("def"), []byte("gh")}, | ||
}, | ||
{ | ||
name: "chunkByte larger than input", | ||
body: []byte("abc"), | ||
chunkByte: 10, | ||
want: [][]byte{[]byte("abc")}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := splitChunk(tt.body, tt.chunkByte) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} | ||
|
||
func randomString(length int) string { | ||
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | ||
var seededRand = rand.New(rand.NewSource(time.Now().UnixNano())) | ||
|
||
b := make([]byte, length) | ||
for i := range b { | ||
b[i] = charset[seededRand.Intn(len(charset))] | ||
} | ||
return string(b) | ||
} | ||
|
||
func TestSplitAndMerge(t *testing.T) { | ||
originalData := []byte(randomString(10000)) | ||
id := "test-id" | ||
chunkSize := 100 | ||
format := "json" | ||
|
||
dataCh := make(chan []byte) | ||
doneCh := make(chan bool) | ||
merger := NewMerger() | ||
|
||
mockProcessFn := func(sequence int, data []byte) (any, error) { | ||
return data, nil | ||
} | ||
|
||
go func() { | ||
err := Split(id, originalData, chunkSize, format, mockProcessFn, func(chunk any) error { | ||
serializedChunk, ok := chunk.([]byte) | ||
if !ok { | ||
return fmt.Errorf("invalid chunk type") | ||
} | ||
|
||
dataCh <- serializedChunk | ||
return nil | ||
}) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
close(dataCh) | ||
}() | ||
|
||
go func() { | ||
for chunk := range dataCh { | ||
combined, completed, err := Merge(merger, chunk, format) | ||
if err != nil { | ||
t.Error(err) | ||
break | ||
} | ||
if completed { | ||
assert.Equal(t, originalData, combined) | ||
doneCh <- true | ||
break | ||
} | ||
} | ||
}() | ||
|
||
<-doneCh | ||
} |
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.