Skip to content

Commit

Permalink
internal/core/adt: rename state constants in sched_test
Browse files Browse the repository at this point in the history
We use a separate set of state flags to test the scheduler.
These use different and more abstract names. However,
on VSCode, at least, the debugger wil show the test
constants instead of the ones in state.go for the
respective bit flags even when testing main code. We
rename the flags to have similar names to ease debugging.
We add a test case to ensure the names are in sync.

Signed-off-by: Marcel van Lohuizen <[email protected]>
Change-Id: I99750f17be2c579bfaf50ab88d7e4c41989ebf00
Dispatch-Trailer: {"type":"trybot","CL":1206316,"patchset":1,"ref":"refs/changes/16/1206316/1","targetBranch":"master"}
  • Loading branch information
mpvl authored and cueckoo committed Dec 24, 2024
1 parent f2c38f8 commit b8a064c
Showing 1 changed file with 68 additions and 43 deletions.
111 changes: 68 additions & 43 deletions internal/core/adt/sched_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,47 @@ import (
"cuelang.org/go/internal/cuetest"
)

// These states used for this test. Each has a suffix of their corresponding
// name in states.go. Debuggers, when resolving constants, will often only
// how the debug constants. Adding the suffix clarifies which states these
// correspond to in the main program.
//
// We could also use the states in the main program directly, but states may
// shift, so this way we ensures that we separate these concerns.
const (
c1 condition = 1 << iota
c2
c3
c4
c1AllAncestorsProcessed condition = 1 << iota
c2ArcTypeKnown
c3ValueKnown
c4ScalarKnown

// auto is a condition that is automatically set by the simulator.
auto
// autoFieldConjunctsKnown is a condition that is automatically set by the simulator.
autoFieldConjunctsKnown
)

func TestStateNames(t *testing.T) {
if c1AllAncestorsProcessed != allAncestorsProcessed {
t.Error("inconsistent state name for allAncestorsProcessed")
}
if c2ArcTypeKnown != arcTypeKnown {
t.Error("inconsistent state name for arcTypeKnown")
}
if c3ValueKnown != valueKnown {
t.Error("inconsistent state name for valueKnown")
}
if c4ScalarKnown != scalarKnown {
t.Error("inconsistent state name for scalarKnown")
}
if autoFieldConjunctsKnown != fieldConjunctsKnown {
t.Error("inconsistent state name for fieldConjunctsKnown")
}
}

// TestScheduler tests the non-CUE specific scheduler functionality.
func TestScheduler(t *testing.T) {
ctx := &OpContext{
Version: internal.DefaultVersion,
taskContext: taskContext{
counterMask: c1 | c2 | c3 | c4,
counterMask: c1AllAncestorsProcessed | c2ArcTypeKnown | c3ValueKnown | c4ScalarKnown,
complete: func(s *scheduler) condition { return 0 },
},
}
Expand Down Expand Up @@ -198,7 +223,7 @@ func TestScheduler(t *testing.T) {
name: "node with one task",
init: func() {
v0 := node(nil)
success("t1", v0, c1, 0)
success("t1", v0, c1AllAncestorsProcessed, 0)
},
log: `
running task t1`,
Expand All @@ -210,8 +235,8 @@ func TestScheduler(t *testing.T) {
name: "node with two tasks",
init: func() {
v0 := node(nil)
success("t1", v0, c1, 0)
success("t2", v0, c2, 0)
success("t1", v0, c1AllAncestorsProcessed, 0)
success("t2", v0, c2ArcTypeKnown, 0)
},
log: `
running task t1
Expand All @@ -225,7 +250,7 @@ func TestScheduler(t *testing.T) {
name: "node failing task",
init: func() {
v0 := node(nil)
fail("t1", v0, c1, 0)
fail("t1", v0, c1AllAncestorsProcessed, 0)
},
log: `
running task t1: FAIL`,
Expand All @@ -241,10 +266,10 @@ func TestScheduler(t *testing.T) {
name: "dependency chain on nodes within scheduler",
init: func() {
v0 := node(nil)
success("third", v0, c3, c2)
success("fourth", v0, c4, c3)
success("second", v0, c2, c1)
success("first", v0, c1, 0)
success("third", v0, c3ValueKnown, c2ArcTypeKnown)
success("fourth", v0, c4ScalarKnown, c3ValueKnown)
success("second", v0, c2ArcTypeKnown, c1AllAncestorsProcessed)
success("first", v0, c1AllAncestorsProcessed, 0)
},
log: `
running task first
Expand All @@ -265,7 +290,7 @@ func TestScheduler(t *testing.T) {
name: "task depends on state for which there is no task",
init: func() {
v0 := node(nil)
success("t1", v0, c2, c1)
success("t1", v0, c2ArcTypeKnown, c1AllAncestorsProcessed)
},
log: `
running task t1`,
Expand All @@ -279,7 +304,7 @@ func TestScheduler(t *testing.T) {
v0 := node(nil)
v1 := node(v0)
v2 := node(v0)
success("t1", v1, c1, 0, dep{node: v2, needs: c2})
success("t1", v1, c1AllAncestorsProcessed, 0, dep{node: v2, needs: c2ArcTypeKnown})
},
log: `
running task t1`,
Expand All @@ -292,10 +317,10 @@ func TestScheduler(t *testing.T) {
name: "tasks depend on multiple other tasks within same scheduler",
init: func() {
v0 := node(nil)
success("before1", v0, c2, 0)
success("last", v0, c4, c1|c2|c3)
success("block", v0, c3, c1|c2)
success("before2", v0, c1, 0)
success("before1", v0, c2ArcTypeKnown, 0)
success("last", v0, c4ScalarKnown, c1AllAncestorsProcessed|c2ArcTypeKnown|c3ValueKnown)
success("block", v0, c3ValueKnown, c1AllAncestorsProcessed|c2ArcTypeKnown)
success("before2", v0, c1AllAncestorsProcessed, 0)
},
log: `
running task before1
Expand Down Expand Up @@ -325,11 +350,11 @@ func TestScheduler(t *testing.T) {
init: func() {
v0 := node(nil)
baz := node(v0)
success("t0", baz, c1, 0)
success("t0", baz, c1AllAncestorsProcessed, 0)
foo := node(v0)

completes("t1:bar", v0, foo, c2, dep{node: baz, needs: c1})
success("t2:baz", v0, c1, 0, dep{node: foo, needs: c2})
completes("t1:bar", v0, foo, c2ArcTypeKnown, dep{node: baz, needs: c1AllAncestorsProcessed})
success("t2:baz", v0, c1AllAncestorsProcessed, 0, dep{node: foo, needs: c2ArcTypeKnown})
},
log: `
running task t1:bar
Expand All @@ -355,11 +380,11 @@ func TestScheduler(t *testing.T) {
init: func() {
v0 := node(nil)
baz := node(v0)
success("foo", baz, c1, 0)
success("foo", baz, c1AllAncestorsProcessed, 0)
foo := node(v0)

success("t2:baz", v0, c1, 0, dep{node: foo, needs: c2})
completes("t1:bar", v0, foo, c2, dep{node: baz, needs: c1})
success("t2:baz", v0, c1AllAncestorsProcessed, 0, dep{node: foo, needs: c2ArcTypeKnown})
completes("t1:bar", v0, foo, c2ArcTypeKnown, dep{node: baz, needs: c1AllAncestorsProcessed})
},
log: `
running task t2:baz
Expand All @@ -380,8 +405,8 @@ func TestScheduler(t *testing.T) {
v0 := node(nil)
v1 := node(v0)
v2 := node(v0)
success("a-10", v1, c1|c2, 0, dep{node: v2, needs: c1})
success("b+10", v2, c1|c2, 0, dep{node: v1, needs: c1})
success("a-10", v1, c1AllAncestorsProcessed|c2ArcTypeKnown, 0, dep{node: v2, needs: c1AllAncestorsProcessed})
success("b+10", v2, c1AllAncestorsProcessed|c2ArcTypeKnown, 0, dep{node: v1, needs: c1AllAncestorsProcessed})
},
log: `
running task a-10
Expand All @@ -405,16 +430,16 @@ func TestScheduler(t *testing.T) {
v0 := node(nil)
v1 := node(v0)
v2 := node(v0)
success("a-10", v1, c1|c2, 0, dep{node: v2, needs: c1})
success("b+10", v2, c1|c2, 0, dep{node: v1, needs: c1})
success("a-10", v1, c1AllAncestorsProcessed|c2ArcTypeKnown, 0, dep{node: v2, needs: c1AllAncestorsProcessed})
success("b+10", v2, c1AllAncestorsProcessed|c2ArcTypeKnown, 0, dep{node: v1, needs: c1AllAncestorsProcessed})

// NOTE: using success("5", v2, c1, 0) here would cause the cyclic
// references to block, as they would both provide and depend on
// v1 and v2 becoming scalars. Once a field is known to be a scalar,
// it can safely be signaled as unification cannot make it more
// concrete. Further unification could result in an error, but that
// will be caught by completing the unification.
signal("5", v2, c1)
signal("5", v2, c1AllAncestorsProcessed)
},
log: `
running task a-10
Expand Down Expand Up @@ -449,8 +474,8 @@ func TestScheduler(t *testing.T) {
init: func() {
x := node(nil)
foo := node(x)
success("5", foo, c1, 0)
success("comprehension", x, c1, 0, dep{node: x, needs: c1})
success("5", foo, c1AllAncestorsProcessed, 0)
success("comprehension", x, c1AllAncestorsProcessed, 0, dep{node: x, needs: c1AllAncestorsProcessed})
},
log: `
running task comprehension
Expand Down Expand Up @@ -479,8 +504,8 @@ func TestScheduler(t *testing.T) {
init: func() {
x := node(nil)
foo := node(x)
success("5", foo, c1, 0)
success("comprehension", x, c1, 0, dep{node: x, needs: c1})
success("5", foo, c1AllAncestorsProcessed, 0)
success("comprehension", x, c1AllAncestorsProcessed, 0, dep{node: x, needs: c1AllAncestorsProcessed})
},
log: `
running task comprehension
Expand Down Expand Up @@ -512,8 +537,8 @@ func TestScheduler(t *testing.T) {
x := node(v0)
y := node(v0)

success("comprehension", x, c1, 0, dep{node: y, needs: c1})
success("comprehension", y, c1, 0, dep{node: x, needs: c1})
success("comprehension", x, c1AllAncestorsProcessed, 0, dep{node: y, needs: c1AllAncestorsProcessed})
success("comprehension", y, c1AllAncestorsProcessed, 0, dep{node: x, needs: c1AllAncestorsProcessed})

},
log: `
Expand Down Expand Up @@ -552,10 +577,10 @@ func TestScheduler(t *testing.T) {
x := node(v0)
y := node(v0)
foo := node(y)
success("5", foo, c1, 0)
success("5", foo, c1AllAncestorsProcessed, 0)

success("comprehension", x, c1, 0, dep{node: y, needs: c1})
success("comprehension", y, c1, 0, dep{node: x, needs: c1})
success("comprehension", x, c1AllAncestorsProcessed, 0, dep{node: y, needs: c1AllAncestorsProcessed})
success("comprehension", y, c1AllAncestorsProcessed, 0, dep{node: x, needs: c1AllAncestorsProcessed})

},
log: `
Expand Down Expand Up @@ -587,11 +612,11 @@ func TestScheduler(t *testing.T) {
// Create and run root scheduler.
tc.init()
for _, n := range nodes {
n.provided |= auto
n.provided |= autoFieldConjunctsKnown
n.signalDoneAdding()
}
for _, n := range nodes {
n.finalize(auto)
n.finalize(autoFieldConjunctsKnown)
}

t.Equal(w.String(), tc.log)
Expand Down

0 comments on commit b8a064c

Please sign in to comment.