|
9 | 9 | package sgbucket
|
10 | 10 |
|
11 | 11 | import (
|
| 12 | + "context" |
12 | 13 | "fmt"
|
13 | 14 | "testing"
|
14 | 15 |
|
| 16 | + "github.com/couchbase/sg-bucket/js" |
15 | 17 | "github.com/stretchr/testify/assert"
|
16 | 18 | )
|
17 | 19 |
|
18 | 20 | // Just verify that the calls to the emit() fn show up in the output.
|
19 | 21 | func TestEmitFunction(t *testing.T) {
|
20 |
| - mapper := NewJSMapFunction(`function(doc) {emit("key", "value"); emit("k2","v2")}`, 0) |
21 |
| - rows, err := mapper.CallFunction(`{}`, "doc1", 0, 0) |
22 |
| - assertNoError(t, err, "CallFunction failed") |
23 |
| - assert.Equal(t, 2, len(rows)) |
24 |
| - assert.Equal(t, &ViewRow{ID: "doc1", Key: "key", Value: "value"}, rows[0]) |
25 |
| - assert.Equal(t, &ViewRow{ID: "doc1", Key: "k2", Value: "v2"}, rows[1]) |
| 22 | + js.TestWithVMPools(t, 4, func(t *testing.T, pool *js.VMPool) { |
| 23 | + mapper := NewJSMapFunction(pool, `function(doc) {emit("key", "value"); emit("k2","v2")}`, 0) |
| 24 | + rows, err := mapper.CallFunction(pool.Context(), `{}`, "doc1", 0, 0) |
| 25 | + assertNoError(t, err, "CallFunction failed") |
| 26 | + assert.Equal(t, 2, len(rows)) |
| 27 | + assert.Equal(t, &ViewRow{ID: "doc1", Key: "key", Value: "value"}, rows[0]) |
| 28 | + assert.Equal(t, &ViewRow{ID: "doc1", Key: "k2", Value: "v2"}, rows[1]) |
| 29 | + }) |
26 | 30 | }
|
27 | 31 |
|
28 | 32 | func TestTimeout(t *testing.T) {
|
29 |
| - mapper := NewJSMapFunction(`function(doc) {while(true) {}}`, 1) |
30 |
| - _, err := mapper.CallFunction(`{}`, "doc1", 0, 0) |
31 |
| - assert.ErrorIs(t, err, ErrJSTimeout) |
| 33 | + js.TestWithVMPools(t, 4, func(t *testing.T, pool *js.VMPool) { |
| 34 | + mapper := NewJSMapFunction(pool, `function(doc) {while(true) {}}`, 1) |
| 35 | + _, err := mapper.CallFunction(pool.Context(), `{}`, "doc1", 0, 0) |
| 36 | + assert.ErrorIs(t, err, context.DeadlineExceeded) |
| 37 | + }) |
32 | 38 | }
|
33 | 39 |
|
34 |
| -func testMap(t *testing.T, mapFn string, doc string) []*ViewRow { |
35 |
| - mapper := NewJSMapFunction(mapFn, 0) |
36 |
| - rows, err := mapper.CallFunction(doc, "doc1", 0, 0) |
| 40 | +func testMap(t *testing.T, host js.ServiceHost, mapFn string, doc string) []*ViewRow { |
| 41 | + mapper := NewJSMapFunction(host, mapFn, 0) |
| 42 | + rows, err := mapper.CallFunction(host.Context(), doc, "doc1", 0, 0) |
37 | 43 | assertNoError(t, err, fmt.Sprintf("CallFunction failed on %s", doc))
|
38 | 44 | return rows
|
39 | 45 | }
|
40 | 46 |
|
41 | 47 | // Now just make sure the input comes through intact
|
42 | 48 | func TestInputParse(t *testing.T) {
|
43 |
| - rows := testMap(t, `function(doc) {emit(doc.key, doc.value);}`, |
44 |
| - `{"key": "k", "value": "v"}`) |
45 |
| - assert.Equal(t, 1, len(rows)) |
46 |
| - assert.Equal(t, &ViewRow{ID: "doc1", Key: "k", Value: "v"}, rows[0]) |
| 49 | + js.TestWithVMPools(t, 4, func(t *testing.T, pool *js.VMPool) { |
| 50 | + rows := testMap(t, pool, `function(doc) {emit(doc.key, doc.value);}`, |
| 51 | + `{"key": "k", "value": "v"}`) |
| 52 | + assert.Equal(t, 1, len(rows)) |
| 53 | + assert.Equal(t, &ViewRow{ID: "doc1", Key: "k", Value: "v"}, rows[0]) |
| 54 | + }) |
47 | 55 | }
|
48 | 56 |
|
49 | 57 | // Test different types of keys/values:
|
50 | 58 | func TestKeyTypes(t *testing.T) {
|
51 |
| - rows := testMap(t, `function(doc) {emit(doc.key, doc.value);}`, |
52 |
| - `{"ID": "doc1", "key": true, "value": false}`) |
53 |
| - assert.Equal(t, &ViewRow{ID: "doc1", Key: true, Value: false}, rows[0]) |
54 |
| - rows = testMap(t, `function(doc) {emit(doc.key, doc.value);}`, |
55 |
| - `{"ID": "doc1", "key": null, "value": 0}`) |
56 |
| - assert.Equal(t, &ViewRow{ID: "doc1", Key: nil, Value: float64(0)}, rows[0]) |
57 |
| - rows = testMap(t, `function(doc) {emit(doc.key, doc.value);}`, |
58 |
| - `{"ID": "doc1", "key": ["foo", 23, []], "value": [null]}`) |
59 |
| - assert.Equal(t, &ViewRow{ |
60 |
| - ID: "doc1", |
61 |
| - Key: []interface{}{"foo", 23.0, []interface{}{}}, |
62 |
| - Value: []interface{}{nil}, |
63 |
| - }, rows[0]) |
64 |
| - |
| 59 | + js.TestWithVMPools(t, 4, func(t *testing.T, pool *js.VMPool) { |
| 60 | + rows := testMap(t, pool, `function(doc) {emit(doc.key, doc.value);}`, |
| 61 | + `{"ID": "doc1", "key": true, "value": false}`) |
| 62 | + assert.Equal(t, &ViewRow{ID: "doc1", Key: true, Value: false}, rows[0]) |
| 63 | + rows = testMap(t, pool, `function(doc) {emit(doc.key, doc.value);}`, |
| 64 | + `{"ID": "doc1", "key": null, "value": 0}`) |
| 65 | + assert.Equal(t, &ViewRow{ID: "doc1", Key: nil, Value: float64(0)}, rows[0]) |
| 66 | + rows = testMap(t, pool, `function(doc) {emit(doc.key, doc.value);}`, |
| 67 | + `{"ID": "doc1", "key": ["foo", 23, []], "value": [null]}`) |
| 68 | + assert.Equal(t, &ViewRow{ |
| 69 | + ID: "doc1", |
| 70 | + Key: []interface{}{"foo", 23.0, []interface{}{}}, |
| 71 | + Value: []interface{}{nil}, |
| 72 | + }, rows[0]) |
| 73 | + }) |
65 | 74 | }
|
66 | 75 |
|
67 | 76 | // Empty/no-op map fn
|
68 | 77 | func TestEmptyJSMapFunction(t *testing.T) {
|
69 |
| - mapper := NewJSMapFunction(`function(doc) {}`, 0) |
70 |
| - rows, err := mapper.CallFunction(`{"key": "k", "value": "v"}`, "doc1", 0, 0) |
71 |
| - assertNoError(t, err, "CallFunction failed") |
72 |
| - assert.Equal(t, 0, len(rows)) |
| 78 | + js.TestWithVMPools(t, 4, func(t *testing.T, pool *js.VMPool) { |
| 79 | + mapper := NewJSMapFunction(pool, `function(doc) {}`, 0) |
| 80 | + rows, err := mapper.CallFunction(pool.Context(), `{"key": "k", "value": "v"}`, "doc1", 0, 0) |
| 81 | + assertNoError(t, err, "CallFunction failed") |
| 82 | + assert.Equal(t, 0, len(rows)) |
| 83 | + }) |
73 | 84 | }
|
74 | 85 |
|
75 | 86 | // Test meta object
|
76 | 87 | func TestMeta(t *testing.T) {
|
77 |
| - mapper := NewJSMapFunction(`function(doc,meta) {if (meta.id!="doc1") throw("bad ID");}`, 0) |
78 |
| - rows, err := mapper.CallFunction(`{"key": "k", "value": "v"}`, "doc1", 0, 0) |
79 |
| - assertNoError(t, err, "CallFunction failed") |
80 |
| - assert.Equal(t, 0, len(rows)) |
| 88 | + js.TestWithVMPools(t, 4, func(t *testing.T, pool *js.VMPool) { |
| 89 | + mapper := NewJSMapFunction(pool, `function(doc,meta) {if (meta.id!="doc1") throw("bad ID");}`, 0) |
| 90 | + rows, err := mapper.CallFunction(pool.Context(), `{"key": "k", "value": "v"}`, "doc1", 0, 0) |
| 91 | + assertNoError(t, err, "CallFunction failed") |
| 92 | + assert.Equal(t, 0, len(rows)) |
| 93 | + }) |
81 | 94 | }
|
82 | 95 |
|
83 | 96 | // Test the public API
|
84 | 97 | func TestPublicJSMapFunction(t *testing.T) {
|
85 |
| - mapper := NewJSMapFunction(`function(doc) {emit(doc.key, doc.value);}`, 0) |
86 |
| - rows, err := mapper.CallFunction(`{"key": "k", "value": "v"}`, "doc1", 0, 0) |
87 |
| - assertNoError(t, err, "CallFunction failed") |
88 |
| - assert.Equal(t, 1, len(rows)) |
89 |
| - assert.Equal(t, &ViewRow{ID: "doc1", Key: "k", Value: "v"}, rows[0]) |
| 98 | + js.TestWithVMPools(t, 4, func(t *testing.T, pool *js.VMPool) { |
| 99 | + mapper := NewJSMapFunction(pool, `function(doc) {emit(doc.key, doc.value);}`, 0) |
| 100 | + rows, err := mapper.CallFunction(pool.Context(), `{"key": "k", "value": "v"}`, "doc1", 0, 0) |
| 101 | + assertNoError(t, err, "CallFunction failed") |
| 102 | + assert.Equal(t, 1, len(rows)) |
| 103 | + assert.Equal(t, &ViewRow{ID: "doc1", Key: "k", Value: "v"}, rows[0]) |
| 104 | + }) |
90 | 105 | }
|
0 commit comments