mirror of
https://github.com/status-im/status-go.git
synced 2025-01-24 21:49:54 +00:00
281b304edb
This change moves our e2e tests into a separate package to make room for proper unit and integration tests. This is Phase 1 described in #371. Changes: Makefile has separate directives to run unit/integration tests and e2e tests, CI runs unit/integration tests first and then e2e tests, E2e tests are in reliability order, i.e. the least reliable tests are run in the end to be sure that nothing else is broken, Some tests are fixed or quarantined.
121 lines
2.1 KiB
Go
121 lines
2.1 KiB
Go
package timers_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/robertkrimen/otto"
|
|
|
|
"github.com/status-im/status-go/geth/jail/internal/loop"
|
|
"github.com/status-im/status-go/geth/jail/internal/timers"
|
|
"github.com/status-im/status-go/geth/jail/internal/vm"
|
|
"github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
func (s *TimersSuite) TestSetTimeout() {
|
|
err := s.vm.Set("__capture", func() {
|
|
s.ch <- struct{}{}
|
|
})
|
|
s.NoError(err)
|
|
|
|
err = s.loop.Eval(`setTimeout(function(n) {
|
|
if (Date.now() - n < 50) {
|
|
throw new Error('timeout was called too soon');
|
|
}
|
|
__capture();
|
|
}, 50, Date.now());`)
|
|
s.NoError(err)
|
|
|
|
select {
|
|
case <-s.ch:
|
|
case <-time.After(1 * time.Second):
|
|
s.Fail("test timed out")
|
|
return
|
|
}
|
|
}
|
|
|
|
func (s *TimersSuite) TestClearTimeout() {
|
|
err := s.vm.Set("__shouldNeverRun", func() {
|
|
s.Fail("should never run")
|
|
})
|
|
s.NoError(err)
|
|
|
|
err = s.loop.Eval(`clearTimeout(setTimeout(function() {
|
|
__shouldNeverRun();
|
|
}, 50));`)
|
|
s.NoError(err)
|
|
|
|
<-time.After(100 * time.Millisecond)
|
|
}
|
|
|
|
func (s *TimersSuite) TestSetInterval() {
|
|
err := s.vm.Set("__done", func() {
|
|
s.ch <- struct{}{}
|
|
})
|
|
s.NoError(err)
|
|
|
|
err = s.loop.Eval(`
|
|
var c = 0;
|
|
var iv = setInterval(function() {
|
|
if (c === 1) {
|
|
clearInterval(iv);
|
|
__done();
|
|
}
|
|
c++;
|
|
}, 50);
|
|
`)
|
|
s.NoError(err)
|
|
|
|
select {
|
|
case <-s.ch:
|
|
value, err := s.vm.Get("c")
|
|
s.NoError(err)
|
|
n, err := value.ToInteger()
|
|
s.NoError(err)
|
|
s.Equal(2, int(n))
|
|
case <-time.After(1 * time.Second):
|
|
s.Fail("test timed out")
|
|
}
|
|
}
|
|
|
|
func (s *TimersSuite) TestClearIntervalImmediately() {
|
|
err := s.vm.Set("__shouldNeverRun", func() {
|
|
s.Fail("should never run")
|
|
})
|
|
s.NoError(err)
|
|
|
|
err = s.loop.Eval(`clearInterval(setInterval(function() {
|
|
__shouldNeverRun();
|
|
}, 50));`)
|
|
s.NoError(err)
|
|
|
|
<-time.After(100 * time.Millisecond)
|
|
}
|
|
|
|
type TimersSuite struct {
|
|
suite.Suite
|
|
|
|
loop *loop.Loop
|
|
vm *vm.VM
|
|
|
|
ch chan struct{}
|
|
}
|
|
|
|
func (s *TimersSuite) SetupTest() {
|
|
o := otto.New()
|
|
s.vm = vm.New(o)
|
|
s.loop = loop.New(s.vm)
|
|
|
|
go s.loop.Run(context.Background())
|
|
|
|
err := timers.Define(s.vm, s.loop)
|
|
s.NoError(err)
|
|
|
|
s.ch = make(chan struct{})
|
|
}
|
|
|
|
func TestTimersSuite(t *testing.T) {
|
|
suite.Run(t, new(TimersSuite))
|
|
}
|