Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from gnufied/backport-deadlock-fix
[4.7] Bug 1936975: Fix deadlock when enqueing functions into the pool
- Loading branch information
Showing
3 changed files
with
47 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package operator | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestThreadPool(t *testing.T) { | ||
pool := NewCheckThreadPool(5, 5) | ||
ctx := context.TODO() | ||
startTime := time.Now() | ||
for i := 0; i < 5; i++ { | ||
i := i | ||
pool.RunGoroutine(ctx, func() { | ||
fmt.Printf("running parent task %d\n", i) | ||
for j := 0; j < 5; j++ { | ||
j := j | ||
runSomeTask(fmt.Sprintf("task-%d-%d", i, j)) | ||
} | ||
}) | ||
} | ||
waitChannel := make(chan interface{}) | ||
go func() { | ||
pool.Wait(ctx) | ||
close(waitChannel) | ||
}() | ||
select { | ||
case <-waitChannel: | ||
fmt.Printf("test finished successfully") | ||
case <-time.After(40 * time.Second): | ||
t.Errorf("test failed to finish") | ||
} | ||
fmt.Printf("time taken is: %v", time.Now().Sub(startTime)) | ||
} | ||
|
||
func runSomeTask(taskID string) { | ||
fmt.Printf("Running task: %s\n", taskID) | ||
time.Sleep(5 * time.Second) | ||
} |