-
Notifications
You must be signed in to change notification settings - Fork 806
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: alanprot <[email protected]>
- Loading branch information
Showing
3 changed files
with
107 additions
and
3 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,86 @@ | ||
package util | ||
|
||
import ( | ||
"bytes" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
"github.com/stretchr/testify/require" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestNewWorkerPool_CreateMultiplesPoolsWithSameRegistry(t *testing.T) { | ||
reg := prometheus.NewPedanticRegistry() | ||
wp1 := NewWorkerPool("test1", 100, reg) | ||
defer wp1.Stop() | ||
wp2 := NewWorkerPool("test2", 100, reg) | ||
defer wp2.Stop() | ||
} | ||
|
||
func TestWorkerPool_TestMetric(t *testing.T) { | ||
reg := prometheus.NewPedanticRegistry() | ||
workerPool := NewWorkerPool("test1", 1, reg) | ||
defer workerPool.Stop() | ||
|
||
require.NoError(t, testutil.GatherAndCompare(reg, bytes.NewBufferString(` | ||
# HELP cortex_worker_pool_fallback_total The total number additional go routines that needed to be created to run jobs. | ||
# TYPE cortex_worker_pool_fallback_total counter | ||
cortex_worker_pool_fallback_total{name="test1"} 0 | ||
`), "cortex_worker_pool_fallback_total")) | ||
|
||
wg := &sync.WaitGroup{} | ||
wg.Add(1) | ||
|
||
// Block the first job | ||
workerPool.Submit(func() { | ||
wg.Wait() | ||
}) | ||
|
||
// create an extra job to increment the metric | ||
workerPool.Submit(func() {}) | ||
require.NoError(t, testutil.GatherAndCompare(reg, bytes.NewBufferString(` | ||
# HELP cortex_worker_pool_fallback_total The total number additional go routines that needed to be created to run jobs. | ||
# TYPE cortex_worker_pool_fallback_total counter | ||
cortex_worker_pool_fallback_total{name="test1"} 1 | ||
`), "cortex_worker_pool_fallback_total")) | ||
|
||
wg.Done() | ||
} | ||
|
||
func TestWorkerPool_ShouldFallbackWhenAllWorkersAreBusy(t *testing.T) { | ||
reg := prometheus.NewPedanticRegistry() | ||
numberOfWorkers := 10 | ||
workerPool := NewWorkerPool("test1", numberOfWorkers, reg) | ||
defer workerPool.Stop() | ||
|
||
m := sync.Mutex{} | ||
blockerWg := sync.WaitGroup{} | ||
blockerWg.Add(numberOfWorkers) | ||
|
||
// Lets lock all submited jobs | ||
m.Lock() | ||
|
||
for i := 0; i < numberOfWorkers; i++ { | ||
workerPool.Submit(func() { | ||
defer blockerWg.Done() | ||
m.Lock() | ||
m.Unlock() | ||
}) | ||
} | ||
|
||
// At this point all workers should be busy. lets try to create a new job | ||
wg := sync.WaitGroup{} | ||
wg.Add(1) | ||
workerPool.Submit(func() { | ||
defer wg.Done() | ||
}) | ||
|
||
// Make sure the last job ran to the end | ||
wg.Wait() | ||
|
||
// Lets release the jobs | ||
m.Unlock() | ||
|
||
blockerWg.Wait() | ||
|
||
} |