engine: resources: Receive keys should match mcl, not golang

The capitalization of these keys was wrong and they weren't getting
seen. Add a test as well.
This commit is contained in:
James Shubin
2024-01-19 18:52:31 -05:00
parent b670bb8d2c
commit 32916f9a6f
8 changed files with 65 additions and 17 deletions

View File

@@ -678,10 +678,10 @@ receiving one. This can _only_ be done inside of the `CheckApply` function!
```golang
// inside CheckApply, probably near the top
if val, exists := obj.init.Recv()["SomeKey"]; exists {
obj.init.Logf("the SomeKey param was sent to us from: %s.%s", val.Res, val.Key)
if val, exists := obj.init.Recv()["some_key"]; exists {
obj.init.Logf("the some_key param was sent to us from: %s.%s", val.Res, val.Key)
if val.Changed {
obj.init.Logf("the SomeKey param was just updated!")
obj.init.Logf("the some_key param was just updated!")
// you may want to invalidate some local cache
}
}

View File

@@ -1226,9 +1226,9 @@ func (obj *FileRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
// might not have a new value to copy, and therefore we won't see this
// notification of change. Therefore, it is important to process these
// promptly, if they must not be lost, such as for cache invalidation.
if val, exists := obj.init.Recv()["Content"]; exists && val.Changed {
if val, exists := obj.init.Recv()["content"]; exists && val.Changed {
// if we received on Content, and it changed, invalidate the cache!
obj.init.Logf("contentCheckApply: invalidating sha256sum of `Content`")
obj.init.Logf("contentCheckApply: invalidating sha256sum of `content`")
obj.sha256sum = "" // invalidate!!
}

View File

@@ -282,9 +282,9 @@ func (obj *KVRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
}
}()
if val, exists := obj.init.Recv()["Value"]; exists && val.Changed {
if val, exists := obj.init.Recv()["value"]; exists && val.Changed {
// if we received on Value, and it changed, wooo, nothing to do.
obj.init.Logf("CheckApply: `Value` was updated!")
obj.init.Logf("CheckApply: `value` was updated!")
}
value, exists, err := obj.kvGet(ctx, obj.getKey())

View File

@@ -85,9 +85,9 @@ func (obj *PrintRes) Watch(ctx context.Context) error {
// CheckApply method for Print resource. Does nothing, returns happy!
func (obj *PrintRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
obj.init.Logf("CheckApply: %t", apply)
if val, exists := obj.init.Recv()["Msg"]; exists && val.Changed {
if val, exists := obj.init.Recv()["msg"]; exists && val.Changed {
// if we received on Msg, and it changed, log message
obj.init.Logf("CheckApply: Received `Msg` of: %s", obj.Msg)
obj.init.Logf("CheckApply: Received `msg` of: %s", obj.Msg)
}
var refresh = obj.init.Refresh()

View File

@@ -96,6 +96,7 @@ type TestRes struct {
AlwaysGroup bool `lang:"alwaysgroup" yaml:"always_group"` // set to true to cause auto grouping
CompareFail bool `lang:"comparefail" yaml:"compare_fail"` // will compare fail?
SendValue string `lang:"sendvalue" yaml:"send_value"` // what value should we send?
ExpectRecv *[]string `lang:"expectrecv" yaml:"expect_recv"` // what keys should we expect from send/recv?
OnlyShow []string `lang:"onlyshow" yaml:"only_show"` // what values do we show?
// TODO: add more fun properties!
@@ -146,14 +147,20 @@ func (obj *TestRes) Watch(ctx context.Context) error {
// CheckApply method for Test resource. Does nothing, returns happy!
func (obj *TestRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
expectRecv := []string{}
for key, val := range obj.init.Recv() {
obj.init.Logf("CheckApply: Received `%s`, changed: %t", key, val.Changed)
expectRecv = append(expectRecv, key)
}
if obj.init.Refresh() {
obj.init.Logf("Received a notification!")
}
if obj.ExpectRecv != nil && len(*obj.ExpectRecv) != len(expectRecv) {
return false, fmt.Errorf("the received keys differ from expected, got: %+v", expectRecv)
}
fakeLogf := func(format string, v ...interface{}) {
key := format[0:strings.LastIndex(format, ":")]
if len(obj.OnlyShow) == 0 || util.StrInList(key, obj.OnlyShow) {
@@ -372,6 +379,19 @@ func (obj *TestRes) Cmp(r engine.Res) error {
if obj.SendValue != res.SendValue {
return fmt.Errorf("the SendValue differs")
}
if (obj.ExpectRecv == nil) != (res.ExpectRecv == nil) { // xor
return fmt.Errorf("the ExpectRecv differs")
}
if obj.ExpectRecv != nil && res.ExpectRecv != nil {
if len(*obj.ExpectRecv) != len(*res.ExpectRecv) {
return fmt.Errorf("the length of ExpectRecv differs")
}
for i, x := range *obj.ExpectRecv {
if x != (*res.ExpectRecv)[i] {
return fmt.Errorf("the item at ExpectRecv index %d differs", i)
}
}
}
if len(obj.OnlyShow) != len(res.OnlyShow) {
return fmt.Errorf("the length of OnlyShow differs")
}

View File

@@ -168,6 +168,7 @@ func TestLowerStructFieldNameToFieldName2(t *testing.T) {
"alwaysgroup": "AlwaysGroup",
"comparefail": "CompareFail",
"sendvalue": "SendValue",
"expectrecv": "ExpectRecv",
"onlyshow": "OnlyShow",
"comment": "Comment",

View File

@@ -126,9 +126,9 @@ func (obj *ValueRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
received := false
different := false
checkOK := false
if val, exists := obj.init.Recv()["Any"]; exists && val.Changed {
if val, exists := obj.init.Recv()["any"]; exists && val.Changed {
// if we received on Any, and it changed, invalidate the cache!
obj.init.Logf("CheckApply: received on `Any`")
obj.init.Logf("CheckApply: received on `any`")
obj.isSet = true // we received something
obj.cachedAny = obj.Any
received = true // we'll always need to send below when we recv

View File

@@ -0,0 +1,27 @@
-- main.mcl --
# send/recv of value1.any into test.msg works!
value "value1" {
any => "i am value1",
}
test "test1" {
sendvalue => "hello from test",
expectrecv => ["anotherstr",], # expecting to recv on these keys!
Meta:autogroup => false,
}
print "print1" {
#msg => "", # gets value from send_value above
}
Value["value1"].any -> Test["test1"].anotherstr
Test["test1"].hello -> Print["print1"].msg
-- OUTPUT --
Edge: test[test1] -> print[print1] # test[test1] -> print[print1]
Edge: value[value1] -> test[test1] # value[value1] -> test[test1]
Field: print[print1].Msg = "hello from test"
Field: test[test1].AnotherStr = "i am value1"
Field: test[test1].ExpectRecv = ["anotherstr"]
Field: test[test1].SendValue = "hello from test"
Field: value[value1].Any = "i am value1"
Vertex: print[print1]
Vertex: test[test1]
Vertex: value[value1]