2017-08-04 16:14:17 +00:00
|
|
|
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)
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(err)
|
2017-09-01 20:17:34 +00:00
|
|
|
require.NotNil(cell)
|
2017-08-04 16:14:17 +00:00
|
|
|
|
|
|
|
// 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());
|
|
|
|
`)
|
2017-08-04 16:14:17 +00:00
|
|
|
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-08-04 16:14:17 +00:00
|
|
|
|
2017-09-01 20:17:34 +00:00
|
|
|
value, err := cell.Get("timerCounts")
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(err)
|
2017-09-01 20:17:34 +00:00
|
|
|
require.True(value.IsNumber())
|
|
|
|
require.Equal("0", value.String())
|
2017-08-04 16:14:17 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 20:17:34 +00:00
|
|
|
func (s *JailTestSuite) TestJailTimeout() {
|
2017-08-04 16:14:17 +00:00
|
|
|
require := s.Require()
|
|
|
|
require.NotNil(s.jail)
|
|
|
|
|
2017-09-01 20:17:34 +00:00
|
|
|
cell, err := s.jail.NewJailCell(testChatID)
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(err)
|
2017-09-01 20:17:34 +00:00
|
|
|
require.NotNil(cell)
|
2017-08-04 16:14:17 +00:00
|
|
|
|
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-08-04 16:14:17 +00:00
|
|
|
|
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-08-04 16:14:17 +00:00
|
|
|
|
2017-09-01 20:17:34 +00:00
|
|
|
value, err := cell.Get("timerCounts")
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(err)
|
2017-09-01 20:17:34 +00:00
|
|
|
require.True(value.IsNumber())
|
|
|
|
require.Equal("1", value.String())
|
2017-08-04 16:14:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *JailTestSuite) TestJailLoopInCall() {
|
|
|
|
require := s.Require()
|
|
|
|
require.NotNil(s.jail)
|
|
|
|
|
2017-08-15 10:27:12 +00:00
|
|
|
s.StartTestNode(params.RopstenNetworkID, true)
|
2017-08-04 16:14:17 +00:00
|
|
|
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")
|
2017-08-04 16:14:17 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|