-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhanced README and added error handling and result retrieval
- Updated README to include sections on task timeout, error handling, and result retrieval. - Modified task function signature to return a result and an error. - Added resultCallback and errorCallback to the goPool struct. - Updated NewGoPool function to accept options for result and error callbacks. - Modified worker start function to handle task results and errors. - Updated tests to reflect changes in task function signature and to test error and result handling. Signed-off-by: Daniel Hu <[email protected]>
- Loading branch information
1 parent
ade1387
commit 5a7dd13
Showing
5 changed files
with
221 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,120 @@ | ||
package gopool | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
"time" | ||
"sync" | ||
"testing" | ||
"time" | ||
"errors" | ||
|
||
"github.com/daniel-hutao/spinlock" | ||
"github.com/daniel-hutao/spinlock" | ||
) | ||
|
||
func TestGoPoolWithMutex(t *testing.T) { | ||
pool := NewGoPool(100, WithLock(new(sync.Mutex))) | ||
for i := 0; i < 1000; i++ { | ||
pool.AddTask(func() { | ||
time.Sleep(10 * time.Millisecond) | ||
}) | ||
} | ||
pool.Release() | ||
pool := NewGoPool(100, WithLock(new(sync.Mutex))) | ||
for i := 0; i < 1000; i++ { | ||
pool.AddTask(func() (interface{}, error) { | ||
time.Sleep(10 * time.Millisecond) | ||
return nil, nil | ||
}) | ||
} | ||
pool.Release() | ||
} | ||
|
||
func TestGoPoolWithSpinLock(t *testing.T) { | ||
pool := NewGoPool(100, WithLock(new(spinlock.SpinLock))) | ||
for i := 0; i < 1000; i++ { | ||
pool.AddTask(func() { | ||
time.Sleep(10 * time.Millisecond) | ||
}) | ||
} | ||
pool.Release() | ||
pool := NewGoPool(100, WithLock(new(spinlock.SpinLock))) | ||
for i := 0; i < 1000; i++ { | ||
pool.AddTask(func() (interface{}, error) { | ||
time.Sleep(10 * time.Millisecond) | ||
return nil, nil | ||
}) | ||
} | ||
pool.Release() | ||
} | ||
|
||
func BenchmarkGoPoolWithMutex(b *testing.B) { | ||
var wg sync.WaitGroup | ||
var taskNum = int(1e6) | ||
pool := NewGoPool(5e4, WithLock(new(sync.Mutex))) | ||
var wg sync.WaitGroup | ||
var taskNum = int(1e6) | ||
pool := NewGoPool(5e4, WithLock(new(sync.Mutex))) | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
wg.Add(taskNum) | ||
for num := 0; num < taskNum; num++ { | ||
pool.AddTask(func() { | ||
time.Sleep(10 * time.Millisecond) | ||
wg.Done() | ||
}) | ||
} | ||
} | ||
wg.Wait() | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
wg.Add(taskNum) | ||
for num := 0; num < taskNum; num++ { | ||
pool.AddTask(func() (interface{}, error) { | ||
time.Sleep(10 * time.Millisecond) | ||
wg.Done() | ||
return nil, nil | ||
}) | ||
} | ||
} | ||
wg.Wait() | ||
b.StopTimer() | ||
pool.Release() | ||
pool.Release() | ||
} | ||
|
||
func BenchmarkGoPoolWithSpinLock(b *testing.B) { | ||
var wg sync.WaitGroup | ||
var taskNum = int(1e6) | ||
pool := NewGoPool(5e4, WithLock(new(spinlock.SpinLock))) | ||
var wg sync.WaitGroup | ||
var taskNum = int(1e6) | ||
pool := NewGoPool(5e4, WithLock(new(spinlock.SpinLock))) | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
wg.Add(taskNum) | ||
for num := 0; num < taskNum; num++ { | ||
pool.AddTask(func() { | ||
time.Sleep(10 * time.Millisecond) | ||
wg.Done() | ||
}) | ||
} | ||
} | ||
wg.Wait() | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
wg.Add(taskNum) | ||
for num := 0; num < taskNum; num++ { | ||
pool.AddTask(func() (interface{}, error) { | ||
time.Sleep(10 * time.Millisecond) | ||
wg.Done() | ||
return nil, nil | ||
}) | ||
} | ||
} | ||
wg.Wait() | ||
b.StopTimer() | ||
pool.Release() | ||
pool.Release() | ||
} | ||
|
||
func BenchmarkGoroutines(b *testing.B) { | ||
var wg sync.WaitGroup | ||
var taskNum = int(1e6) | ||
var wg sync.WaitGroup | ||
var taskNum = int(1e6) | ||
|
||
for i := 0; i < b.N; i++ { | ||
wg.Add(taskNum) | ||
for num := 0; num < taskNum; num++ { | ||
go func() (interface{}, error) { | ||
time.Sleep(10 * time.Millisecond) | ||
wg.Done() | ||
return nil, nil | ||
}() | ||
} | ||
} | ||
} | ||
|
||
for i := 0; i < b.N; i++ { | ||
wg.Add(taskNum) | ||
for num := 0; num < taskNum; num++ { | ||
go func() { | ||
time.Sleep(10 * time.Millisecond) | ||
wg.Done() | ||
}() | ||
func TestGoPoolWithError(t *testing.T) { | ||
var errTaskError = errors.New("task error") | ||
pool := NewGoPool(100, WithErrorCallback(func(err error) { | ||
if err != errTaskError { | ||
t.Errorf("Expected error %v, but got %v", errTaskError, err) | ||
} | ||
})) | ||
for i := 0; i< 1000; i++ { | ||
pool.AddTask(func() (interface{}, error) { | ||
return nil, errTaskError | ||
}) | ||
} | ||
pool.Release() | ||
} | ||
|
||
func TestGoPoolWithResult(t *testing.T) { | ||
var expectedResult = "task result" | ||
pool := NewGoPool(100, WithResultCallback(func(result interface{}) { | ||
if result != expectedResult { | ||
t.Errorf("Expected result %v, but got %v", expectedResult, result) | ||
} | ||
})) | ||
for i := 0; i< 1000; i++ { | ||
pool.AddTask(func() (interface{}, error) { | ||
return expectedResult, nil | ||
}) | ||
} | ||
pool.Release() | ||
} |
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