Skip to content

Commit c9c1dcb

Browse files
committed
transcode/nginx tests, examples.
1 parent b17edac commit c9c1dcb

28 files changed

+980
-799
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ nginx
44
misc/tp_dump
55
misc/tp_send
66
misc/json2tp
7+
*.snap
8+
*.xlog
9+
tc.out*
10+
test/cases/*.tmp

Makefile

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ yajl:
1818
ln -sf src third_party/yajl/yajl
1919
cd $(YAJL_PATH); ./configure; make distro
2020

21-
build:
21+
build: utils
2222
$(MAKE) -C $(NGX_PATH)
2323

2424
configure-debug:
@@ -30,7 +30,7 @@ configure-debug:
3030
--with-debug --with-ld-opt='$(LDFLAGS)'
3131
mkdir -p $(PREFIX_PATH)/conf $(PREFIX_PATH)/logs
3232
cp -Rf $(NGX_PATH)/conf/* $(PREFIX_PATH)/conf
33-
rm -f $(PREFIX_PATH)/conf/nginx.conf $(PREFIX_PATH)/conf/nginx.dev.conf
33+
rm -f $(PREFIX_PATH)/conf/nginx.conf
3434
ln -s $(PWD)/misc/nginx.dev.conf $(PREFIX_PATH)/conf/nginx.conf > /dev/null
3535

3636
configure:
@@ -40,27 +40,26 @@ configure:
4040
--prefix=$(PREFIX_PATH)
4141

4242
json2tp:
43-
$(CC) $(CFLAGS) $(INC_FLAGS) $(LDFLAGS) -lyajl_s \
43+
$(CC) $(CFLAGS) $(INC_FLAGS) $(LDFLAGS) -I$(CUR_PATH) -lyajl_s \
4444
$(CUR_PATH)/misc/json2tp.c \
4545
tp_transcode.c \
4646
-o misc/json2tp
4747

4848
tp_dump:
49-
$(CC) $(CFLAGS) $(INC_FLAGS) $(LDFLAGS) -lyajl_s \
49+
$(CC) $(CFLAGS) $(INC_FLAGS) $(LDFLAGS) -I$(CUR_PATH) -lyajl_s \
5050
$(CUR_PATH)/misc/tp_dump.c \
5151
tp_transcode.c \
5252
-o misc/tp_dump
53-
tp_send:
54-
$(CC) $(CFLAGS) $(INC_FLAGS) $(LDFLAGS) -lyajl_s \
55-
$(CUR_PATH)/misc/tp_send.c \
56-
tp_transcode.c \
57-
-o misc/tp_send
53+
54+
test: utils build
55+
$(CUR_PATH)/test/transcode.sh
56+
$(CUR_PATH)/test/nginx-tnt.sh
5857

5958
clean:
6059
$(MAKE) -C $(NGX_PATH) clean
6160
rm -f misc/tp_{send,dump} misc/json2tp
6261

63-
utils: json2tp tp_dump tp_send
62+
utils: json2tp tp_dump
6463
build-all: yajl configure build utils
6564
build-all-debug: yajl configure-debug build utils
6665

conf/nginx.conf

Lines changed: 0 additions & 102 deletions
This file was deleted.

examples/echo.html

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,68 @@
1+
<!DOCTYPE html>
12
<html>
23
<head>
34
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
5+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
6+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
7+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
48
</head>
59

610
<body>
11+
<div class='container' style='margin-top:50px;'>
12+
<div class="input-group input-group-lg">
13+
<input type='text' class='form-control' placeholder='{}' aria-describedby='sizing-addon1' id='input'>
14+
<span class='input-group-btn'>
15+
<button type='button' class='btn' id='submit'>execute</button>
16+
</span>
17+
</div>
18+
<div>
19+
<pre id='output'></pre>
20+
</div>
21+
</div>
722

823
<script>
924
$(document).ready(function() {
10-
$.ajax({
11-
type: "POST",
12-
url: "tnt",
13-
data: JSON.stringify(
14-
{method:"tester_select", params:["hello from ajax!"],id:555})
15-
}).done(
16-
function(d) {
17-
alert(d);
18-
}
19-
).fail(
20-
function(e) {
21-
alert(e);
22-
}
23-
);
25+
26+
var id = 0;
27+
var $out = $('#output');
28+
29+
function render(data) {
30+
$out.append('<div>[' + data.id + ']' + JSON.stringify(data) + '</div>');
31+
}
32+
33+
function render_error(data, stat, xhr) {
34+
if (data.status &&
35+
(data.status === 502 || data.status == 500))
36+
{
37+
$out.append('<div>tarantool backend failure</div>');
38+
}
2439
}
25-
);
26-
</script>
2740

28-
<span> Result: </span>
29-
<div id='result'></div>
41+
$("#submit").click(function() {
42+
43+
try {
44+
var $input = $('#input');
45+
var call = JSON.stringify({
46+
method: 'call',
47+
params: ['echo', [JSON.parse($input.val())]],
48+
id: ++id
49+
});
50+
51+
$.ajax({ type: 'POST',
52+
contentType: 'application/json; charset=utf-8',
53+
dataType: 'json',
54+
url: 'tnt',
55+
data: call,
56+
success: render,
57+
error: render_error});
58+
59+
} catch (e) {
60+
$out.append('<div> Error:' + e + '</div>');
61+
}
62+
});
63+
64+
});
65+
</script>
3066

3167
</body>
3268
</html>

examples/echo.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
json=require('json');
2+
3+
function echo(a)
4+
if type(a) == 'table' then
5+
return {{a}}
6+
end
7+
return {a}
8+
end
9+
10+
box.cfg {
11+
log_level = 5;
12+
listen = 9999;
13+
}
14+
15+
if not box.space.tester then
16+
box.schema.user.grant('guest', 'read,write,execute', 'universe')
17+
box.schema.create_space('tester')
18+
end

misc/json2tp.c

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <errno.h>
55
#include <getopt.h>
66

7-
#include "tp.h"
87
#include "tp_transcode.h"
98

109
int
@@ -13,76 +12,89 @@ main(int argc, char **argv)
1312
FILE *file = stdin;
1413
FILE *out_file = stdout;
1514
size_t size = 1024*4;
16-
char *data = (char *) calloc(1, size), *output = (char *) calloc(1, size);
17-
18-
if (data == NULL || output == NULL) {
19-
fprintf(stderr,
20-
"Failed to allocate read/output buffer of %zu bytes, exiting.",
21-
size*2);
22-
exit(2);
23-
}
2415

2516
int c;
2617
static struct option long_options[] = {
2718
{"in-file", required_argument, 0, 'i'},
2819
{"out-file", required_argument, 0, 'o'},
20+
{"size", required_argument, 0, 's'},
2921
{0, 0, 0, 0}
3022
};
3123
int index = 0;
32-
while ((c = getopt_long(argc, argv, "io:", long_options, &index)) != -1) {
24+
while ((c = getopt_long(argc, argv, "ios:", long_options, &index)) != -1) {
3325
switch(c) {
3426
case 'i':
3527
file = fopen(optarg, "r");
3628
if (file == NULL) {
37-
fprintf(stderr, "Failed to open in-file '%s', error '%s'\n",
29+
fprintf(stderr, "json2tp: "
30+
"failed to open in-file '%s', error '%s'\n",
3831
optarg, strerror(errno));
3932
exit(2);
4033
}
4134
break;
4235
case 'o':
4336
out_file = fopen(optarg, "w");
4437
if (out_file == NULL) {
45-
fprintf(stderr, "Failed to open out-file '%s', error '%s'\n",
38+
fprintf(stderr, "json2tp: "
39+
"failed to open out-file '%s', error '%s'\n",
4640
optarg, strerror(errno));
4741
exit(2);
4842
}
4943
break;
44+
case 's':
45+
size = (size_t)atoi(optarg);
46+
break;
5047
default:
51-
break;
48+
fprintf(stderr, "json2tp: unknown option\n");
49+
exit(2);
5250
}
5351
}
5452

53+
char *data = (char *) calloc(1, 1024*2), *output = (char *) calloc(1, size);
54+
if (data == NULL || output == NULL) {
55+
fprintf(stderr,
56+
"json2tp: failed to allocate buffer of %zu bytes, exiting.",
57+
size*2);
58+
exit(2);
59+
}
60+
5561
tp_transcode_t t;
5662
if (tp_transcode_init(&t, output, size, YAJL_JSON_TO_TP)
5763
== TP_TRANSCODE_ERROR)
58-
{
59-
fprintf(stderr, "Failed to initialize transcode, exiting\n");
64+
{
65+
fprintf(stderr, "json2tp: failed to initialize transcode, exiting\n");
6066
exit(2);
6167
}
6268

69+
int rc = 0;
6370
for (size_t s = 0;;) {
6471
char *it = data;
6572
const size_t rd = fread((void *) data, 1, 10, file);
6673
s += rd;
6774
if (rd == 0) {
6875
if (!feof(file)) {
69-
fprintf(stderr, "error reading from\n");
76+
fprintf(stderr, "json2tp: error reading from\n");
7077
}
7178
break;
7279
}
7380

74-
if (tp_transcode(&t, it, rd) == TP_TRANSCODE_ERROR) {
75-
fprintf(stderr, "Failed to transcode: '%s'\n", t.errmsg);
81+
if ((rc = tp_transcode(&t, it, rd)) == TP_TRANSCODE_ERROR) {
82+
fprintf(stderr, "json2tp: failed to transcode: '%s'\n", t.errmsg);
7683
break;
7784
}
7885
it = data + size;
7986
}
8087

81-
size_t complete_msg_size = 0;
82-
if (tp_transcode_complete(&t, &complete_msg_size) != TP_TRANSCODE_ERROR)
83-
fwrite(output, 1, complete_msg_size, out_file);
84-
else
85-
fprintf(stderr, "Failed to complete: '%s'\n", t.errmsg);
88+
if (rc == TP_TRANSCODE_OK) {
89+
size_t complete_msg_size = 0;
90+
if (tp_transcode_complete(&t, &complete_msg_size)
91+
!= TP_TRANSCODE_ERROR)
92+
fwrite(output, 1, complete_msg_size, out_file);
93+
else
94+
fprintf(stderr, "json2tp: failed to complete: '%s'\n", t.errmsg);
95+
}
96+
97+
tp_transcode_free(&t);
8698

8799
fflush(out_file);
88100
fflush(stderr);

0 commit comments

Comments
 (0)