-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.cpp
116 lines (93 loc) · 3.01 KB
/
test.cpp
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
#include "../src/extism.hpp"
#include <fstream>
#include <thread>
#include <gtest/gtest.h>
std::vector<uint8_t> read(const char *filename) {
std::ifstream file(filename, std::ios::binary);
return std::vector<uint8_t>((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
}
const std::string code = "../wasm/code.wasm";
namespace {
using namespace extism;
TEST(Plugin, Manifest) {
Manifest manifest = Manifest::wasmPath(code);
manifest.setConfig("a", "1");
Plugin plugin(manifest);
Buffer buf = plugin.call("count_vowels", "this is a test");
ASSERT_TRUE(buf.string().find("\"count\":4") != std::string::npos);
}
TEST(Plugin, ManifestWasmUrl) {
const auto manifest =
extism::Manifest::wasmURL("https://github.com/extism/plugins/releases/"
"latest/download/count_vowels.wasm");
ASSERT_NO_THROW(extism::Plugin plugin(manifest, true));
}
TEST(Plugin, BadManifest) {
Manifest manifest;
ASSERT_THROW(Plugin plugin(manifest), Error);
}
TEST(Plugin, Bytes) {
auto wasm = read(code.c_str());
ASSERT_NO_THROW(Plugin plugin(wasm));
Plugin plugin(wasm);
Buffer buf = plugin.call("count_vowels", "this is another test");
ASSERT_TRUE(buf.string().find("\"count\":6") != std::string::npos);
}
TEST(Plugin, UpdateConfig) {
auto wasm = read(code.c_str());
Plugin plugin(wasm);
Config config;
config["abc"] = "123";
ASSERT_NO_THROW(plugin.config(config));
}
TEST(Plugin, FunctionExists) {
auto wasm = read(code.c_str());
Plugin plugin(wasm);
ASSERT_FALSE(plugin.functionExists("bad_function"));
ASSERT_TRUE(plugin.functionExists("count_vowels"));
}
TEST(Plugin, HostFunction) {
auto wasm = read("../wasm/code-functions.wasm");
auto t = std::vector<ValType>{ValType::ExtismValType_I64};
Function hello_world =
Function("hello_world", t, t, [](CurrentPlugin plugin, void *user_data) {
plugin.output(std::string("test"));
});
auto functions = std::vector<Function>{
hello_world,
};
Plugin plugin(wasm, true, functions);
auto buf = plugin.call("count_vowels", "aaa");
ASSERT_EQ(buf.length, 4);
ASSERT_EQ((std::string)buf, "test");
}
void callThread(Plugin *plugin) {
auto buf = plugin->call("count_vowels", "aaa").string();
ASSERT_EQ(buf.size(), 10);
ASSERT_EQ(buf, "testing123");
}
TEST(Plugin, MultipleThreads) {
auto wasm = read("../wasm/code-functions.wasm");
auto t = std::vector<ValType>{ValType::ExtismValType_I64};
Function hello_world =
Function("hello_world", t, t, [](CurrentPlugin plugin, void *user_data) {
plugin.output(std::string("testing123"));
});
auto functions = std::vector<Function>{
hello_world,
};
Plugin plugin(wasm, true, functions);
std::vector<std::thread> threads;
for (int i = 0; i < 3; i++) {
threads.push_back(std::thread(callThread, &plugin));
}
for (auto &th : threads) {
th.join();
}
}
}; // namespace
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}