|
| 1 | +package engine_test |
| 2 | + |
| 3 | +import ( |
| 4 | + r "github.com/stretchr/testify/require" |
| 5 | + "mokapi/engine" |
| 6 | + "mokapi/engine/common" |
| 7 | + "mokapi/engine/enginetest" |
| 8 | + "mokapi/runtime/events" |
| 9 | + "mokapi/runtime/metrics" |
| 10 | + "mokapi/runtime/runtimetest" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +func TestEngine_Scheduler(t *testing.T) { |
| 16 | + testcases := []struct { |
| 17 | + name string |
| 18 | + test func(t *testing.T, e *engine.Engine, c *metrics.Counter) |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "run job", |
| 22 | + test: func(t *testing.T, e *engine.Engine, c *metrics.Counter) { |
| 23 | + err := e.AddScript(newScript("test.js", ` |
| 24 | + import mokapi from 'mokapi' |
| 25 | + export default function() { |
| 26 | + mokapi.every('1s', function() { mokapi.sleep(1); }); |
| 27 | + } |
| 28 | + `)) |
| 29 | + r.NoError(t, err) |
| 30 | + |
| 31 | + time.Sleep(300 * time.Millisecond) |
| 32 | + |
| 33 | + evts := events.GetEvents(events.NewTraits().WithNamespace("job").WithName("test.js")) |
| 34 | + r.Len(t, evts, 1) |
| 35 | + exec := evts[0].Data.(common.JobExecution) |
| 36 | + r.Equal(t, "test.js", exec.Tags["name"]) |
| 37 | + r.Greater(t, exec.Duration, int64(0)) |
| 38 | + r.Equal(t, "1s", exec.Schedule) |
| 39 | + |
| 40 | + r.Equal(t, float64(1), c.Value()) |
| 41 | + }, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "run job with name", |
| 45 | + test: func(t *testing.T, e *engine.Engine, c *metrics.Counter) { |
| 46 | + err := e.AddScript(newScript("test.js", ` |
| 47 | + import mokapi from 'mokapi' |
| 48 | + export default function() { |
| 49 | + mokapi.every('1s', function() {}, { tags: { name: 'foo' } }); |
| 50 | + } |
| 51 | + `)) |
| 52 | + r.NoError(t, err) |
| 53 | + |
| 54 | + time.Sleep(300 * time.Millisecond) |
| 55 | + |
| 56 | + evts := events.GetEvents(events.NewTraits().WithNamespace("job")) |
| 57 | + r.Len(t, evts, 1) |
| 58 | + exec := evts[0].Data.(common.JobExecution) |
| 59 | + r.Equal(t, "foo", exec.Tags["name"]) |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "run job with custom tag", |
| 64 | + test: func(t *testing.T, e *engine.Engine, c *metrics.Counter) { |
| 65 | + err := e.AddScript(newScript("test.js", ` |
| 66 | + import mokapi from 'mokapi' |
| 67 | + export default function() { |
| 68 | + mokapi.every('1s', function() {}, { tags: { foo: 'bar' } }); |
| 69 | + } |
| 70 | + `)) |
| 71 | + r.NoError(t, err) |
| 72 | + |
| 73 | + time.Sleep(300 * time.Millisecond) |
| 74 | + |
| 75 | + evts := events.GetEvents(events.NewTraits().WithNamespace("job")) |
| 76 | + r.Len(t, evts, 1) |
| 77 | + exec := evts[0].Data.(common.JobExecution) |
| 78 | + r.Equal(t, "bar", exec.Tags["foo"]) |
| 79 | + }, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "run job with logs", |
| 83 | + test: func(t *testing.T, e *engine.Engine, c *metrics.Counter) { |
| 84 | + err := e.AddScript(newScript("test.js", ` |
| 85 | + import mokapi from 'mokapi' |
| 86 | + export default function() { |
| 87 | + mokapi.every('1s', function() { |
| 88 | + console.log('a log message'); |
| 89 | + }); |
| 90 | + } |
| 91 | + `)) |
| 92 | + r.NoError(t, err) |
| 93 | + |
| 94 | + time.Sleep(300 * time.Millisecond) |
| 95 | + |
| 96 | + evts := events.GetEvents(events.NewTraits().WithNamespace("job")) |
| 97 | + r.Len(t, evts, 1) |
| 98 | + exec := evts[0].Data.(common.JobExecution) |
| 99 | + r.Len(t, exec.Logs, 1) |
| 100 | + r.Equal(t, "a log message", exec.Logs[0].Message) |
| 101 | + }, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "run job script error", |
| 105 | + test: func(t *testing.T, e *engine.Engine, c *metrics.Counter) { |
| 106 | + err := e.AddScript(newScript("test.js", ` |
| 107 | + import mokapi from 'mokapi' |
| 108 | + export default function() { |
| 109 | + mokapi.every('1s', function() { |
| 110 | + throw new Error('script error'); |
| 111 | + }); |
| 112 | + } |
| 113 | + `)) |
| 114 | + r.NoError(t, err) |
| 115 | + |
| 116 | + time.Sleep(300 * time.Millisecond) |
| 117 | + |
| 118 | + evts := events.GetEvents(events.NewTraits().WithNamespace("job")) |
| 119 | + r.Len(t, evts, 1) |
| 120 | + exec := evts[0].Data.(common.JobExecution) |
| 121 | + r.NotNil(t, exec.Error) |
| 122 | + r.Equal(t, "Error: script error at test.js:5:13(3)", exec.Error.Message) |
| 123 | + }, |
| 124 | + }, |
| 125 | + } |
| 126 | + |
| 127 | + for _, tc := range testcases { |
| 128 | + t.Run(tc.name, func(t *testing.T) { |
| 129 | + app := runtimetest.NewApp() |
| 130 | + app.Monitor.JobCounter = &metrics.Counter{} |
| 131 | + e := enginetest.NewEngine( |
| 132 | + engine.WithScheduler(engine.NewDefaultScheduler()), |
| 133 | + engine.WithApp(app), |
| 134 | + ) |
| 135 | + defer e.Close() |
| 136 | + e.Start() |
| 137 | + |
| 138 | + events.SetStore(10, events.NewTraits().WithNamespace("job")) |
| 139 | + |
| 140 | + tc.test(t, e, app.Monitor.JobCounter) |
| 141 | + |
| 142 | + events.Reset() |
| 143 | + }) |
| 144 | + } |
| 145 | +} |
0 commit comments