Skip to content

Commit

Permalink
db: ensure vm interupt is cleared
Browse files Browse the repository at this point in the history
Signed-off-by: Sander Pick <[email protected]>
  • Loading branch information
sanderpick committed Feb 26, 2022
1 parent b74267e commit 9fc2d3d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
10 changes: 7 additions & 3 deletions db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var (

baseKey = dsPrefix.ChildString("collection")

vmTimeout = time.Second * 30
vmTimeout = time.Second
)

const (
Expand Down Expand Up @@ -341,13 +341,15 @@ func (c *Collection) validWrite(identity thread.PubKey, e core.Event) error {
return fmt.Errorf("parsing instance in validate write: %v", err)
}
}
time.AfterFunc(vmTimeout, func() {
c.vm.ClearInterrupt()
timer := time.AfterFunc(vmTimeout, func() {
c.vm.Interrupt("validator timed out")
})
res, err := c.writeValidator(nil, writer, event, inv)
if err != nil {
return fmt.Errorf("running write validator func: %v", err)
}
timer.Stop()
out := res.Export()
switch out.(type) {
case bool:
Expand Down Expand Up @@ -377,13 +379,15 @@ func (c *Collection) filterRead(identity thread.PubKey, instance []byte) ([]byte
if err != nil {
return nil, fmt.Errorf("parsing instance in filter read: %v", err)
}
time.AfterFunc(vmTimeout, func() {
c.vm.ClearInterrupt()
timer := time.AfterFunc(vmTimeout, func() {
c.vm.Interrupt("filter timed out")
})
res, err := c.readFilter(nil, reader, inv)
if err != nil {
return nil, fmt.Errorf("running read filter func: %v", err)
}
timer.Stop()
out := res.Export()
switch out.(type) {
case nil:
Expand Down
30 changes: 30 additions & 0 deletions db/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ func TestNewCollection(t *testing.T) {
if err == nil {
t.Fatal("should get validator timed out error")
}

// test clear interupt
c, err = db.UpdateCollection(CollectionConfig{
Name: "Dog",
Schema: util.SchemaFromInstance(&Dog{}, false),
WriteValidator: `
return true
`,
})
checkErr(t, err)
id, err = c.Create(util.JSONFromInstance(dog))
checkErr(t, err)
time.Sleep(time.Second) // sleep vm timeout
id, err = c.Create(util.JSONFromInstance(dog))
checkErr(t, err)
})
t.Run("WithReadFilter", func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -196,6 +211,21 @@ func TestNewCollection(t *testing.T) {
if err == nil {
t.Fatal("should get filter timed out error")
}

// test clear interupt
c, err = db.UpdateCollection(CollectionConfig{
Name: "Dog",
Schema: util.SchemaFromInstance(&Dog{}, false),
ReadFilter: `
return instance
`,
})
checkErr(t, err)
_, err = c.FindByID(id)
checkErr(t, err)
time.Sleep(time.Second) // sleep vm timeout
_, err = c.FindByID(id)
checkErr(t, err)
})
t.Run("SingleExpandedSchemaStruct", func(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit 9fc2d3d

Please sign in to comment.