Prioritize checking consumer context cancellation

Tests expect that as soon as eventer shuts down immediately on context
cancellations; but golang does not guarantee priority when multiple
pending channels are ready in a select statement.
This commit is contained in:
Mahmood Ali 2018-11-06 12:39:21 -08:00
parent c62ec124c0
commit 7690f389a0
2 changed files with 11 additions and 1 deletions

View file

@ -549,7 +549,7 @@ func waitTilNodeReady(client *Client, t *testing.T) {
func TestClient_SaveRestoreState(t *testing.T) {
t.Parallel()
ctestutil.ExecCompatible(t)
s1, _ := testServer(t, nil)
defer s1.Shutdown()
testutil.WaitForLeader(t, s1.RPC)
@ -573,6 +573,7 @@ func TestClient_SaveRestoreState(t *testing.T) {
alloc1.Job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
"run_for": "10s",
}
alloc1.ClientStatus = structs.AllocClientStatusRunning
state := s1.State()
if err := state.UpsertJob(100, job); err != nil {

View file

@ -81,6 +81,15 @@ func (e *Eventer) iterateConsumers(event *drivers.TaskEvent) {
e.consumersLock.Lock()
filtered := e.consumers[:0]
for _, consumer := range e.consumers {
// prioritize checking if context is cancelled prior
// to attempting to forwarding events
// golang select evaluations aren't predictable
if consumer.ctx.Err() != nil {
close(consumer.ch)
continue
}
select {
case <-time.After(consumer.timeout):
filtered = append(filtered, consumer)