-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add testing for Timeout, Submit and Sharding
- Loading branch information
1 parent
7f767ea
commit 41b6b68
Showing
6 changed files
with
216 additions
and
13 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
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,134 @@ | ||
package gojob_test | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/WangYihang/gojob" | ||
) | ||
|
||
type SafeWriter struct { | ||
writer *strings.Builder | ||
lock sync.Mutex | ||
} | ||
|
||
func NewSafeWriter() *SafeWriter { | ||
return &SafeWriter{ | ||
writer: new(strings.Builder), | ||
lock: sync.Mutex{}, | ||
} | ||
} | ||
|
||
func (sw *SafeWriter) WriteString(s string) { | ||
sw.lock.Lock() | ||
defer sw.lock.Unlock() | ||
sw.writer.WriteString(s) | ||
} | ||
|
||
func (sw *SafeWriter) String() string { | ||
return sw.writer.String() | ||
} | ||
|
||
type Task struct { | ||
I int | ||
writer *SafeWriter | ||
} | ||
|
||
func NewTask(i int, writer *SafeWriter) *Task { | ||
return &Task{ | ||
I: i, | ||
writer: writer, | ||
} | ||
} | ||
|
||
func (t *Task) Do() error { | ||
t.writer.WriteString(fmt.Sprintf("%d\n", t.I)) | ||
return nil | ||
} | ||
|
||
func TestRunWithTimeout(t *testing.T) { | ||
task := func() error { | ||
time.Sleep(2 * time.Second) | ||
return nil | ||
} | ||
|
||
err := gojob.RunWithTimeout(task, 1*time.Second) | ||
if err == nil { | ||
t.Errorf("Expected timeout error, got nil") | ||
} | ||
} | ||
|
||
func TestSchedulerSubmit(t *testing.T) { | ||
scheduler := gojob.NewScheduler().SetNumShards(2).SetShard(1) | ||
safeWriter := NewSafeWriter() | ||
task := NewTask(1, safeWriter) | ||
scheduler.Submit(task) | ||
if scheduler.NumTasks.Load() != 1 { | ||
t.Errorf("Expected NumTasks to be 1, got %d", scheduler.NumTasks.Load()) | ||
} | ||
} | ||
|
||
func TestSharding(t *testing.T) { | ||
testcases := []struct { | ||
numShards int64 | ||
shard int64 | ||
expected []int | ||
}{ | ||
{ | ||
numShards: 2, | ||
shard: 0, | ||
expected: []int{0, 2, 4, 6, 8, 10, 12, 14}, | ||
}, | ||
{ | ||
numShards: 2, | ||
shard: 1, | ||
expected: []int{1, 3, 5, 7, 9, 11, 13, 15}, | ||
}, | ||
{ | ||
numShards: 3, | ||
shard: 0, | ||
expected: []int{0, 3, 6, 9, 12, 15}, | ||
}, | ||
{ | ||
numShards: 3, | ||
shard: 1, | ||
expected: []int{1, 4, 7, 10, 13}, | ||
}, | ||
{ | ||
numShards: 3, | ||
shard: 2, | ||
expected: []int{2, 5, 8, 11, 14}, | ||
}, | ||
} | ||
for _, tc := range testcases { | ||
safeWriter := NewSafeWriter() | ||
scheduler := gojob.NewScheduler().SetNumShards(tc.numShards).SetShard(tc.shard).SetOutputFilePath("").Start() | ||
for i := 0; i < 16; i++ { | ||
scheduler.Submit(NewTask(i, safeWriter)) | ||
} | ||
scheduler.Wait() | ||
output := safeWriter.String() | ||
lines := strings.Split(output, "\n") | ||
numbers := []int{} | ||
for _, line := range lines { | ||
if line == "" { | ||
continue | ||
} | ||
number, err := strconv.Atoi(line) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
numbers = append(numbers, number) | ||
} | ||
sort.Ints(numbers) | ||
if !reflect.DeepEqual(numbers, tc.expected) { | ||
t.Errorf("Expected %v, got %v", tc.expected, numbers) | ||
} | ||
} | ||
} |
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,44 @@ | ||
package util | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
) | ||
|
||
type OutputCapture struct { | ||
originalStdout *os.File | ||
r *os.File | ||
w *os.File | ||
buffer bytes.Buffer | ||
} | ||
|
||
func NewOutputCapture() *OutputCapture { | ||
return &OutputCapture{} | ||
} | ||
|
||
func (oc *OutputCapture) StartCapture() { | ||
oc.originalStdout = os.Stdout | ||
|
||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
oc.r = r | ||
oc.w = w | ||
} | ||
|
||
func (oc *OutputCapture) StopCapture() { | ||
os.Stdout = oc.originalStdout | ||
oc.w.Close() | ||
|
||
io.Copy(&oc.buffer, oc.r) | ||
oc.r.Close() | ||
} | ||
|
||
func (oc *OutputCapture) GetCapturedOutput() string { | ||
return oc.buffer.String() | ||
} | ||
|
||
// capture := util.NewOutputCapture() | ||
// capture.StartCapture() | ||
// capture.StopCapture() |