z_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package mjs
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "os"
  8. "testing"
  9. "time"
  10. "github.com/dop251/goja"
  11. )
  12. var contentPath = "content"
  13. func filePath(name string) string {
  14. return fmt.Sprintf("%v/%v", contentPath, name)
  15. }
  16. func modified(name string) (res int64) {
  17. if info, err := os.Stat(filePath(name)); err == nil {
  18. res = info.ModTime().Unix()
  19. }
  20. return
  21. }
  22. func content(name string) ([]byte, error) {
  23. return ioutil.ReadFile(filePath(name))
  24. }
  25. func TestMJS(t *testing.T) {
  26. mjs := New(modified, content)
  27. for i := 0; i < 20; i++ {
  28. modified, err := mjs.Exec("main", map[string]interface{}{
  29. "flush": func(args ...interface{}) {
  30. log.Println(args)
  31. },
  32. }, time.Second*300)
  33. t.Log(err, err == nil, modified)
  34. }
  35. }
  36. type CallResult struct {
  37. }
  38. func Call(runtime *goja.Runtime, call goja.Callable) {
  39. log.Println("CALL_START", runtime, call)
  40. if call == nil {
  41. log.Println(errors.New("OKKO"))
  42. log.Println("CALL_FINIFH - nil")
  43. return
  44. }
  45. result, err := call(goja.Undefined())
  46. log.Println("CALL_FINIFH - nil", result, err)
  47. //return errors.New("ERRRRROR")
  48. }
  49. func TestInterrupt(t *testing.T) {
  50. mjs := New(modified, content)
  51. for i := 0; i < 2; i++ {
  52. modified, err := mjs.Exec("interrupt", map[string]interface{}{
  53. "call": Call,
  54. "flush": func(args ...interface{}) {
  55. log.Println(args)
  56. },
  57. }, time.Second*5)
  58. t.Log(err, err == nil, modified)
  59. }
  60. time.Sleep(time.Second * 10)
  61. }