status-go/geth/jail/jail_cell_test.go

117 lines
2.5 KiB
Go
Raw Normal View History

package jail_test
import (
"time"
"github.com/robertkrimen/otto"
"github.com/status-im/status-go/geth/params"
)
func (s *JailTestSuite) TestJailTimeoutFailure() {
require := s.Require()
require.NotNil(s.jail)
2017-09-01 20:17:34 +00:00
cell, err := s.jail.NewJailCell(testChatID)
require.NoError(err)
2017-09-01 20:17:34 +00:00
require.NotNil(cell)
// Attempt to run a timeout string against a JailCell.
2017-09-01 20:17:34 +00:00
_, err = cell.Run(`
var timerCounts = 0;
setTimeout(function(n){
if (Date.now() - n < 50) {
throw new Error("Timed out");
}
timerCounts++;
}, 30, Date.now());
`)
require.NoError(err)
2017-09-01 20:17:34 +00:00
// wait at least 10x longer to decrease probability
// of false negatives as we using real clock here
time.Sleep(300 * time.Millisecond)
2017-09-01 20:17:34 +00:00
value, err := cell.Get("timerCounts")
require.NoError(err)
2017-09-01 20:17:34 +00:00
require.True(value.IsNumber())
require.Equal("0", value.String())
}
2017-09-01 20:17:34 +00:00
func (s *JailTestSuite) TestJailTimeout() {
require := s.Require()
require.NotNil(s.jail)
2017-09-01 20:17:34 +00:00
cell, err := s.jail.NewJailCell(testChatID)
require.NoError(err)
2017-09-01 20:17:34 +00:00
require.NotNil(cell)
2017-09-01 20:17:34 +00:00
// Attempt to run a timeout string against a JailCell.
_, err = cell.Run(`
var timerCounts = 0;
setTimeout(function(n){
if (Date.now() - n < 50) {
throw new Error("Timed out");
}
timerCounts++;
}, 50, Date.now());
`)
require.NoError(err)
2017-09-01 20:17:34 +00:00
// wait at least 10x longer to decrease probability
// of false negatives as we using real clock here
time.Sleep(300 * time.Millisecond)
2017-09-01 20:17:34 +00:00
value, err := cell.Get("timerCounts")
require.NoError(err)
2017-09-01 20:17:34 +00:00
require.True(value.IsNumber())
require.Equal("1", value.String())
}
func (s *JailTestSuite) TestJailLoopInCall() {
require := s.Require()
require.NotNil(s.jail)
s.StartTestNode(params.RopstenNetworkID, true)
defer s.StopTestNode()
// load Status JS and add test command to it
s.jail.BaseJS(baseStatusJSCode)
s.jail.Parse(testChatID, ``)
cell, err := s.jail.GetCell(testChatID)
require.NoError(err)
require.NotNil(cell)
items := make(chan string)
err = cell.Set("__captureResponse", func(val string) otto.Value {
go func() { items <- val }()
return otto.UndefinedValue()
})
require.NoError(err)
_, err = cell.Run(`
function callRunner(namespace){
console.log("Initiating callRunner for: ", namespace)
return setTimeout(function(){
__captureResponse(namespace);
}, 1000);
}
`)
require.NoError(err)
2017-09-01 20:17:34 +00:00
_, err = cell.Call("callRunner", nil, "softball")
require.NoError(err)
select {
case received := <-items:
require.Equal(received, "softball")
break
case <-time.After(5 * time.Second):
require.Fail("Failed to received event response")
}
}