Skip to content

Commit

Permalink
Add test for circular resouces
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Sep 20, 2024
1 parent 60aeec6 commit 9dbe246
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions runtime/tests/interpreter/reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3291,3 +3291,73 @@ func TestInterpretHostFunctionReferenceInvalidation(t *testing.T) {
AssertValuesEqual(t, inter, expectedResult, result)
})
}

func TestInterpretCreatingCircularDependentResource(t *testing.T) {

t.Parallel()

t.Run("resource container field", func(t *testing.T) {
t.Parallel()

inter := parseCheckAndInterpret(t, `
access(all) resource A {
access(mapping Identity) var b: @[B]
init() {
self.b <- []
}
}
access(all) resource B {
access(all) let a: @A
init(_ a: @A) {
self.a <- a
}
}
access(all) fun main() {
var a <- create A()
var b <- create B(<-a)
let aRef = &b.a as auth(Mutate) &A
aRef.b.append(<-b)
}
`)

_, err := inter.Invoke("main")
RequireError(t, err)
assert.ErrorAs(t, err, &interpreter.InvalidatedResourceReferenceError{})
})

t.Run("resource field", func(t *testing.T) {
t.Parallel()

inter := parseCheckAndInterpret(t, `
access(all) resource A {
access(self) var b: @B?
init() {
self.b <- nil
}
access(all) fun setB(_ b: @B) {
self.b <-! b
}
}
access(all) resource B {
access(all) let a: @A
init(_ a: @A) {
self.a <- a
}
}
access(all) fun main() {
var a <- create A()
var b <- create B(<-a)
let aRef = &b.a as auth(Mutate) &A
aRef.setB(<-b)
}
`)

_, err := inter.Invoke("main")
RequireError(t, err)
assert.ErrorAs(t, err, &interpreter.InvalidatedResourceReferenceError{})
})
}

0 comments on commit 9dbe246

Please sign in to comment.