-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supporting use of context.Context to time out execution of the solver
- Loading branch information
Showing
8 changed files
with
159 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package centipede | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
) | ||
|
||
var ( | ||
ErrExecutionCanceled error = errors.New("execution canceled") | ||
) | ||
|
||
// RunWithContext accepts a context and a function that produces T | ||
// The function will be run, and so long as the context is not done, | ||
// its result will be returned. Otherwise an error will be returned. | ||
func RunWithContext[T any](ctx context.Context, f func() T) (*T, error) { | ||
ch := make(chan T, 1) | ||
go func() { | ||
r := f() | ||
ch <- r | ||
}() | ||
|
||
select { | ||
case res := <-ch: | ||
return &res, nil | ||
case <-ctx.Done(): | ||
return nil, ErrExecutionCanceled | ||
} | ||
} |
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
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,80 @@ | ||
// Copyright 2022 Gabriel Boorse | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package centipede | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTimeout(t *testing.T) { | ||
vars := Variables[int]{ | ||
NewVariable("A", IntRange(1, 10)), | ||
NewVariable("B", IntRange(1, 10)), | ||
NewVariable("C", IntRange(1, 10)), | ||
} | ||
|
||
constraints := Constraints[int]{ | ||
Equals[int]("A", "B"), // A = B | ||
Constraint[int]{Vars: VariableNames{"A", "B"}, | ||
ConstraintFunction: func(variables *Variables[int]) bool { | ||
time.Sleep(10 * time.Millisecond) | ||
if variables.Find("A").Empty || variables.Find("B").Empty { | ||
return true | ||
} | ||
return variables.Find("A").Value > variables.Find("B").Value | ||
}}, | ||
} | ||
|
||
// solve the problem | ||
solver := NewBackTrackingCSPSolver(vars, constraints) | ||
d := time.Now().Add(20 * time.Millisecond) | ||
ctx, cancel := context.WithDeadline(context.TODO(), d) | ||
defer cancel() | ||
_, err := solver.Solve(ctx) | ||
assert.Equal(t, ErrExecutionCanceled, err) | ||
} | ||
|
||
func TestNoTimeout(t *testing.T) { | ||
vars := Variables[int]{ | ||
NewVariable("A", IntRange(1, 10)), | ||
NewVariable("B", IntRange(1, 10)), | ||
NewVariable("C", IntRange(1, 10)), | ||
} | ||
|
||
constraints := Constraints[int]{ | ||
Equals[int]("A", "B"), // A = B | ||
Constraint[int]{Vars: VariableNames{"A", "C"}, | ||
ConstraintFunction: func(variables *Variables[int]) bool { | ||
time.Sleep(1 * time.Millisecond) | ||
if variables.Find("A").Empty || variables.Find("C").Empty { | ||
return true | ||
} | ||
return variables.Find("A").Value > variables.Find("C").Value | ||
}}, | ||
} | ||
|
||
// solve the problem | ||
solver := NewBackTrackingCSPSolver(vars, constraints) | ||
d := time.Now().Add(200 * time.Millisecond) | ||
ctx, cancel := context.WithDeadline(context.TODO(), d) | ||
defer cancel() | ||
success, err := solver.Solve(ctx) | ||
assert.Nil(t, err) | ||
assert.True(t, success) | ||
} |
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