1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
# to run (requires ruby and rspec):
# spec test/query_server_spec.rb -f specdoc --color
COUCH_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(COUCH_ROOT)
LANGUAGE = ENV["QS_LANG"] || "js"
puts "Running query server specs for #{LANGUAGE} query server"
require 'spec'
require 'json'
class OSProcessRunner
def self.run
trace = ENV["QS_TRACE"] || false
puts "launching #{run_command}" if trace
if block_given?
IO.popen(run_command, "r+") do |io|
qs = QueryServerRunner.new(io, trace)
yield qs
end
else
io = IO.popen(run_command, "r+")
QueryServerRunner.new(io, trace)
end
end
def initialize io, trace = false
@qsio = io
@trace = trace
end
def close
@qsio.close
end
def reset!
run(["reset"])
end
def add_fun(fun)
run(["add_fun", fun])
end
def get_chunks
resp = jsgets
raise "not a chunk" unless resp.first == "chunks"
return resp[1]
end
def run json
rrun json
jsgets
end
def rrun json
line = json.to_json
puts "run: #{line}" if @trace
@qsio.puts line
end
def rgets
resp = @qsio.gets
puts "got: #{resp}" if @trace
resp
end
def jsgets
resp = rgets
# err = @qserr.gets
# puts "err: #{err}" if err
if resp
begin
rj = JSON.parse("[#{resp.chomp}]")[0]
rescue JSON::ParserError
puts "JSON ERROR (dump under trace mode)"
# puts resp.chomp
while resp = rgets
# puts resp.chomp
end
end
if rj.respond_to?(:[]) && rj.is_a?(Array)
if rj[0] == "log"
log = rj[1]
puts "log: #{log}" if @trace
rj = jsgets
end
end
rj
else
raise "no response"
end
end
end
class QueryServerRunner < OSProcessRunner
COMMANDS = {
"js" => "#{COUCH_ROOT}/src/couchdb/couchjs #{COUCH_ROOT}/share/server/main.js",
"erlang" => "#{COUCH_ROOT}/test/run_native_process.es"
}
def self.run_command
COMMANDS[LANGUAGE]
end
end
class ExternalRunner < OSProcessRunner
def self.run_command
"#{COUCH_ROOT}/src/couchdb/couchjs #{COUCH_ROOT}/share/server/echo.js"
end
end
functions = {
"emit-twice" => {
"js" => %{function(doc){emit("foo",doc.a); emit("bar",doc.a)}},
"erlang" => <<-ERLANG
fun({Doc}) ->
A = proplists:get_value(<<"a">>, Doc, null),
Emit(<<"foo">>, A),
Emit(<<"bar">>, A)
end.
ERLANG
},
"emit-once" => {
"js" => %{function(doc){emit("baz",doc.a)}},
"erlang" => <<-ERLANG
fun({Doc}) ->
A = proplists:get_value(<<"a">>, Doc, null),
Emit(<<"baz">>, A)
end.
ERLANG
},
"reduce-values-length" => {
"js" => %{function(keys, values, rereduce) { return values.length; }},
"erlang" => %{fun(Keys, Values, ReReduce) -> length(Values) end.}
},
"reduce-values-sum" => {
"js" => %{function(keys, values, rereduce) { return sum(values); }},
"erlang" => %{fun(Keys, Values, ReReduce) -> lists:sum(Values) end.}
},
"validate-forbidden" => {
"js" => <<-JS,
function(newDoc, oldDoc, userCtx) {
if(newDoc.bad)
throw({forbidden:"bad doc"}); "foo bar";
}
JS
"erlang" => <<-ERLANG
fun({NewDoc}, _OldDoc, _UserCtx) ->
case proplists:get_value(<<"bad">>, NewDoc) of
undefined -> 1;
_ -> {[{forbidden, <<"bad doc">>}]}
end
end.
ERLANG
},
"show-simple" => {
"js" => <<-JS,
function(doc, req) {
log("ok");
return [doc.title, doc.body].join(' - ');
}
JS
"erlang" => <<-ERLANG
fun({Doc}, Req) ->
Title = proplists:get_value(<<"title">>, Doc),
Body = proplists:get_value(<<"body">>, Doc),
Resp = <<Title/binary, " - ", Body/binary>>,
{[{<<"body">>, Resp}]}
end.
ERLANG
},
"show-headers" => {
"js" => <<-JS,
function(doc, req) {
var resp = {"code":200, "headers":{"X-Plankton":"Rusty"}};
resp.body = [doc.title, doc.body].join(' - ');
return resp;
}
JS
"erlang" => <<-ERLANG
fun({Doc}, Req) ->
Title = proplists:get_value(<<"title">>, Doc),
Body = proplists:get_value(<<"body">>, Doc),
Resp = <<Title/binary, " - ", Body/binary>>,
{[
{<<"code">>, 200},
{<<"headers">>, {[{<<"X-Plankton">>, <<"Rusty">>}]}},
{<<"body">>, Resp}
]}
end.
ERLANG
},
"show-sends" => {
"js" => <<-JS,
function(head, req) {
start({headers:{"Content-Type" : "text/plain"}});
send("first chunk");
send('second "chunk"');
return "tail";
};
JS
"erlang" => <<-ERLANG
fun(Head, Req) ->
Resp = {[
{<<"headers">>, {[{<<"Content-Type">>, <<"text/plain">>}]}}
]},
Start(Resp),
Send(<<"first chunk">>),
Send(<<"second \\\"chunk\\\"">>),
<<"tail">>
end.
ERLANG
},
"show-while-get-rows" => {
"js" => <<-JS,
function(head, req) {
send("first chunk");
send(req.q);
var row;
log("about to getRow " + typeof(getRow));
while(row = getRow()) {
send(row.key);
};
return "tail";
};
JS
"erlang" => <<-ERLANG,
fun(Head, {Req}) ->
Send(<<"first chunk">>),
Send(proplists:get_value(<<"q">>, Req)),
Fun = fun({Row}, _) ->
Send(proplists:get_value(<<"key">>, Row)),
{ok, nil}
end,
{ok, _} = FoldRows(Fun, nil),
<<"tail">>
end.
ERLANG
},
"show-while-get-rows-multi-send" => {
"js" => <<-JS,
function(head, req) {
send("bacon");
var row;
log("about to getRow " + typeof(getRow));
while(row = getRow()) {
send(row.key);
send("eggs");
};
return "tail";
};
JS
"erlang" => <<-ERLANG,
fun(Head, Req) ->
Send(<<"bacon">>),
Fun = fun({Row}, _) ->
Send(proplists:get_value(<<"key">>, Row)),
Send(<<"eggs">>),
{ok, nil}
end,
FoldRows(Fun, nil),
<<"tail">>
end.
ERLANG
},
"list-simple" => {
"js" => <<-JS,
function(head, req) {
send("first chunk");
send(req.q);
var row;
while(row = getRow()) {
send(row.key);
};
return "early";
};
JS
"erlang" => <<-ERLANG,
fun(Head, {Req}) ->
Send(<<"first chunk">>),
Send(proplists:get_value(<<"q">>, Req)),
Fun = fun({Row}, _) ->
Send(proplists:get_value(<<"key">>, Row)),
{ok, nil}
end,
FoldRows(Fun, nil),
<<"early">>
end.
ERLANG
},
"list-chunky" => {
"js" => <<-JS,
function(head, req) {
send("first chunk");
send(req.q);
var row, i=0;
while(row = getRow()) {
send(row.key);
i += 1;
if (i > 2) {
return('early tail');
}
};
};
JS
"erlang" => <<-ERLANG,
fun(Head, {Req}) ->
Send(<<"first chunk">>),
Send(proplists:get_value(<<"q">>, Req)),
Fun = fun
({Row}, Count) when Count < 2 ->
Send(proplists:get_value(<<"key">>, Row)),
{ok, Count+1};
({Row}, Count) when Count == 2 ->
Send(proplists:get_value(<<"key">>, Row)),
{stop, <<"early tail">>}
end,
{ok, Tail} = FoldRows(Fun, 0),
Tail
end.
ERLANG
},
"list-old-style" => {
"js" => <<-JS,
function(head, req, foo, bar) {
return "stuff";
}
JS
"erlang" => <<-ERLANG,
fun(Head, Req, Foo, Bar) ->
<<"stuff">>
end.
ERLANG
},
"list-capped" => {
"js" => <<-JS,
function(head, req) {
send("bacon")
var row, i = 0;
while(row = getRow()) {
send(row.key);
i += 1;
if (i > 2) {
return('early');
}
};
}
JS
"erlang" => <<-ERLANG,
fun(Head, Req) ->
Send(<<"bacon">>),
Fun = fun
({Row}, Count) when Count < 2 ->
Send(proplists:get_value(<<"key">>, Row)),
{ok, Count+1};
({Row}, Count) when Count == 2 ->
Send(proplists:get_value(<<"key">>, Row)),
{stop, <<"early">>}
end,
{ok, Tail} = FoldRows(Fun, 0),
Tail
end.
ERLANG
},
"list-raw" => {
"js" => <<-JS,
function(head, req) {
send("first chunk");
send(req.q);
var row;
while(row = getRow()) {
send(row.key);
};
return "tail";
};
JS
"erlang" => <<-ERLANG,
fun(Head, {Req}) ->
Send(<<"first chunk">>),
Send(proplists:get_value(<<"q">>, Req)),
Fun = fun({Row}, _) ->
Send(proplists:get_value(<<"key">>, Row)),
{ok, nil}
end,
FoldRows(Fun, nil),
<<"tail">>
end.
ERLANG
},
"filter-basic" => {
"js" => <<-JS,
function(doc, req) {
if (doc.good) {
return true;
}
}
JS
"erlang" => <<-ERLANG,
fun({Doc}, Req) ->
proplists:get_value(<<"good">>, Doc)
end.
ERLANG
},
"update-basic" => {
"js" => <<-JS,
function(doc, req) {
doc.world = "hello";
var resp = [doc, "hello doc"];
return resp;
}
JS
"erlang" => <<-ERLANG,
fun({Doc}, Req) ->
Doc2 = [{<<"world">>, <<"hello">>}|Doc],
[{Doc2}, {[{<<"body">>, <<"hello doc">>}]}]
end.
ERLANG
}
}
describe "query server normal case" do
before(:all) do
`cd #{COUCH_ROOT} && make`
@qs = QueryServerRunner.run
end
after(:all) do
@qs.close
end
it "should reset" do
@qs.run(["reset"]).should == true
end
it "should run map funs" do
@qs.reset!
@qs.run(["add_fun", functions["emit-twice"][LANGUAGE]]).should == true
@qs.run(["add_fun", functions["emit-once"][LANGUAGE]]).should == true
rows = @qs.run(["map_doc", {:a => "b"}])
rows[0][0].should == ["foo", "b"]
rows[0][1].should == ["bar", "b"]
rows[1][0].should == ["baz", "b"]
end
describe "reduce" do
before(:all) do
@fun = functions["reduce-values-length"][LANGUAGE]
@qs.reset!
end
it "should reduce" do
kvs = (0...10).collect{|i|[i,i*2]}
@qs.run(["reduce", [@fun], kvs]).should == [true, [10]]
end
end
describe "rereduce" do
before(:all) do
@fun = functions["reduce-values-sum"][LANGUAGE]
@qs.reset!
end
it "should rereduce" do
vs = (0...10).collect{|i|i}
@qs.run(["rereduce", [@fun], vs]).should == [true, [45]]
end
end
# it "should validate"
describe "validation" do
before(:all) do
@fun = functions["validate-forbidden"][LANGUAGE]
@qs.reset!
end
it "should allow good updates" do
@qs.run(["validate", @fun, {"good" => true}, {}, {}]).should == 1
end
it "should reject invalid updates" do
@qs.run(["validate", @fun, {"bad" => true}, {}, {}]).should == {"forbidden"=>"bad doc"}
end
end
describe "show" do
before(:all) do
@fun = functions["show-simple"][LANGUAGE]
@qs.reset!
end
it "should show" do
@qs.rrun(["show", @fun,
{:title => "Best ever", :body => "Doc body"}, {}])
@qs.jsgets.should == ["resp", {"body" => "Best ever - Doc body"}]
end
end
describe "show with headers" do
before(:all) do
@fun = functions["show-headers"][LANGUAGE]
@qs.reset!
end
it "should show headers" do
@qs.rrun(["show", @fun,
{:title => "Best ever", :body => "Doc body"}, {}])
@qs.jsgets.should == ["resp", {"code"=>200,"headers" => {"X-Plankton"=>"Rusty"}, "body" => "Best ever - Doc body"}]
end
end
# end
# LIST TESTS
# __END__
describe "raw list with headers" do
before(:each) do
@fun = functions["show-sends"][LANGUAGE]
@qs.reset!
@qs.add_fun(@fun).should == true
end
it "should do headers proper" do
@qs.rrun(["list", {"total_rows"=>1000}, {"q" => "ok"}])
@qs.jsgets.should == ["start", ["first chunk", 'second "chunk"'], {"headers"=>{"Content-Type"=>"text/plain"}}]
@qs.rrun(["list_end"])
@qs.jsgets.should == ["end", ["tail"]]
end
end
describe "list with rows" do
before(:each) do
@fun = functions["show-while-get-rows"][LANGUAGE]
@qs.run(["reset"]).should == true
@qs.add_fun(@fun).should == true
end
it "should list em" do
@qs.rrun(["list", {"foo"=>"bar"}, {"q" => "ok"}])
@qs.jsgets.should == ["start", ["first chunk", "ok"], {"headers"=>{}}]
@qs.rrun(["list_row", {"key"=>"baz"}])
@qs.get_chunks.should == ["baz"]
@qs.rrun(["list_row", {"key"=>"bam"}])
@qs.get_chunks.should == ["bam"]
@qs.rrun(["list_end"])
@qs.jsgets.should == ["end", ["tail"]]
end
it "should work with zero rows" do
@qs.rrun(["list", {"foo"=>"bar"}, {"q" => "ok"}])
@qs.jsgets.should == ["start", ["first chunk", "ok"], {"headers"=>{}}]
@qs.rrun(["list_end"])
@qs.jsgets.should == ["end", ["tail"]]
end
end
describe "should buffer multiple chunks sent for a single row." do
before(:all) do
@fun = functions["show-while-get-rows-multi-send"][LANGUAGE]
@qs.reset!
@qs.add_fun(@fun).should == true
end
it "should should buffer em" do
@qs.rrun(["list", {"foo"=>"bar"}, {"q" => "ok"}])
@qs.jsgets.should == ["start", ["bacon"], {"headers"=>{}}]
@qs.rrun(["list_row", {"key"=>"baz"}])
@qs.get_chunks.should == ["baz", "eggs"]
@qs.rrun(["list_row", {"key"=>"bam"}])
@qs.get_chunks.should == ["bam", "eggs"]
@qs.rrun(["list_end"])
@qs.jsgets.should == ["end", ["tail"]]
end
end
describe "example list" do
before(:all) do
@fun = functions["list-simple"][LANGUAGE]
@qs.reset!
@qs.add_fun(@fun).should == true
end
it "should run normal" do
@qs.run(["list", {"foo"=>"bar"}, {"q" => "ok"}]).should == ["start", ["first chunk", "ok"], {"headers"=>{}}]
@qs.run(["list_row", {"key"=>"baz"}]).should == ["chunks", ["baz"]]
@qs.run(["list_row", {"key"=>"bam"}]).should == ["chunks", ["bam"]]
@qs.run(["list_row", {"key"=>"foom"}]).should == ["chunks", ["foom"]]
@qs.run(["list_row", {"key"=>"fooz"}]).should == ["chunks", ["fooz"]]
@qs.run(["list_row", {"key"=>"foox"}]).should == ["chunks", ["foox"]]
@qs.run(["list_end"]).should == ["end" , ["early"]]
end
end
describe "only goes to 2 list" do
before(:all) do
@fun = functions["list-chunky"][LANGUAGE]
@qs.reset!
@qs.add_fun(@fun).should == true
end
it "should end early" do
@qs.run(["list", {"foo"=>"bar"}, {"q" => "ok"}]).
should == ["start", ["first chunk", "ok"], {"headers"=>{}}]
@qs.run(["list_row", {"key"=>"baz"}]).
should == ["chunks", ["baz"]]
@qs.run(["list_row", {"key"=>"bam"}]).
should == ["chunks", ["bam"]]
@qs.run(["list_row", {"key"=>"foom"}]).
should == ["end", ["foom", "early tail"]]
# here's where js has to discard quit properly
@qs.run(["reset"]).
should == true
end
end
describe "changes filter" do
before(:all) do
@fun = functions["filter-basic"][LANGUAGE]
@qs.reset!
@qs.add_fun(@fun).should == true
end
it "should only return true for good docs" do
@qs.run(["filter", [{"key"=>"bam", "good" => true}, {"foo" => "bar"}, {"good" => true}], {"req" => "foo"}]).
should == [true, [true, false, true]]
end
end
describe "update" do
before(:all) do
@fun = functions["update-basic"][LANGUAGE]
@qs.reset!
end
it "should return a doc and a resp body" do
up, doc, resp = @qs.run(["update", @fun, {"foo" => "gnarly"}, {"verb" => "POST"}])
up.should == "up"
doc.should == {"foo" => "gnarly", "world" => "hello"}
resp["body"].should == "hello doc"
end
end
end
def should_have_exited qs
begin
qs.run(["reset"])
"raise before this".should == true
rescue RuntimeError => e
e.message.should == "no response"
rescue Errno::EPIPE
true.should == true
end
end
describe "query server that exits" do
before(:each) do
@qs = QueryServerRunner.run
end
after(:each) do
@qs.close
end
if LANGUAGE == "js"
describe "old style list" do
before(:each) do
@fun = functions["list-old-style"][LANGUAGE]
@qs.reset!
@qs.add_fun(@fun).should == true
end
it "should get a warning" do
resp = @qs.run(["list", {"foo"=>"bar"}, {"q" => "ok"}])
resp["error"].should == "render_error"
resp["reason"].should include("the list API has changed")
end
end
end
describe "only goes to 2 list" do
before(:each) do
@fun = functions["list-capped"][LANGUAGE]
@qs.reset!
@qs.add_fun(@fun).should == true
end
it "should exit if erlang sends too many rows" do
@qs.run(["list", {"foo"=>"bar"}, {"q" => "ok"}]).should == ["start", ["bacon"], {"headers"=>{}}]
@qs.run(["list_row", {"key"=>"baz"}]).should == ["chunks", ["baz"]]
@qs.run(["list_row", {"key"=>"foom"}]).should == ["chunks", ["foom"]]
@qs.run(["list_row", {"key"=>"fooz"}]).should == ["end", ["fooz", "early"]]
@qs.rrun(["list_row", {"key"=>"foox"}])
@qs.jsgets["error"].should == "query_server_error"
should_have_exited @qs
end
end
describe "raw list" do
before(:each) do
@fun = functions["list-raw"][LANGUAGE]
@qs.run(["reset"]).should == true
@qs.add_fun(@fun).should == true
end
it "should exit if it gets a non-row in the middle" do
@qs.rrun(["list", {"foo"=>"bar"}, {"q" => "ok"}])
@qs.jsgets.should == ["start", ["first chunk", "ok"], {"headers"=>{}}]
@qs.run(["reset"])["error"].should == "query_server_error"
should_have_exited @qs
end
end
end
|