Skip to content

Commit

Permalink
Merge pull request #577 from textileio/sander/bump-vmtimeout
Browse files Browse the repository at this point in the history
move vm timeout to where func is called
  • Loading branch information
sanderpick authored Feb 25, 2022
2 parents a512e0a + 9630d43 commit b74267e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
11 changes: 7 additions & 4 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.Millisecond * 200
vmTimeout = time.Second * 30
)

const (
Expand Down Expand Up @@ -87,9 +87,6 @@ func newCollection(d *DB, config CollectionConfig) (*Collection, error) {
return nil, err
}
vm := goja.New()
time.AfterFunc(vmTimeout, func() {
vm.Interrupt("validator timed out")
})
wv := []byte(config.WriteValidator)
rf := []byte(config.ReadFilter)
c := &Collection{
Expand Down Expand Up @@ -344,6 +341,9 @@ 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.Interrupt("validator timed out")
})
res, err := c.writeValidator(nil, writer, event, inv)
if err != nil {
return fmt.Errorf("running write validator func: %v", err)
Expand Down Expand Up @@ -377,6 +377,9 @@ 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.Interrupt("filter timed out")
})
res, err := c.readFilter(nil, reader, inv)
if err != nil {
return nil, fmt.Errorf("running read filter func: %v", err)
Expand Down
29 changes: 29 additions & 0 deletions db/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,21 @@ func TestNewCollection(t *testing.T) {
if err == nil {
t.Fatal("delete should have been invalid")
}

// test vm interupt
c, err = db.UpdateCollection(CollectionConfig{
Name: "Dog",
Schema: util.SchemaFromInstance(&Dog{}, false),
WriteValidator: `
while(true){}
`,
})
checkErr(t, err)
dog = Dog{Name: "Fido", Comments: []Comment{}}
id, err = c.Create(util.JSONFromInstance(dog))
if err == nil {
t.Fatal("should get validator timed out error")
}
})
t.Run("WithReadFilter", func(t *testing.T) {
t.Parallel()
Expand All @@ -167,6 +182,20 @@ func TestNewCollection(t *testing.T) {
if filtered.Name != "Clyde" {
t.Fatal("name should have been modified by read filter")
}

// test vm interupt
c, err = db.UpdateCollection(CollectionConfig{
Name: "Dog",
Schema: util.SchemaFromInstance(&Dog{}, false),
ReadFilter: `
while(true){}
`,
})
checkErr(t, err)
_, err = c.FindByID(id)
if err == nil {
t.Fatal("should get filter timed out error")
}
})
t.Run("SingleExpandedSchemaStruct", func(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit b74267e

Please sign in to comment.