summaryrefslogtreecommitdiffstats
path: root/tests/BlockTypeRegistry/BlockTypeRegistryTest.cpp
blob: cac83c6a8deef82047e64c059eabe1d94784d195 (plain) (blame)
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

#include "Globals.h"
#include "Bindings/BlockTypeRegistry.h"
#include "../TestHelpers.h"




/** Dummy BlockState implementation */
class BlockState
{
public:
	BlockState() = default;
};




/** Dummy cBlockHandler implementation that allows simple checking for equality through mIdent. */
class cBlockHandler
{
public:
	cBlockHandler(UInt32 aIdent):
		mIdent(aIdent)
	{
	}

	UInt32 mIdent;
};





/** Tests simple block type name registration.
Registers a block type, checks that the type is then registered. */
static void testSimpleReg()
{
	LOGD("Testing simple registration...");

	// Register the block type:
	BlockTypeRegistry reg;
	AString blockTypeName("test:block1");
	AString pluginName("testPlugin");
	AString hint1("testHint1");
	AString hint1Value("value1");
	std::shared_ptr<cBlockHandler> handler(new cBlockHandler(0x12345678));
	std::map<AString, AString> hints = {{hint1, hint1Value}, {"testHint2", "value2"}};
	std::map<AString, BlockInfo::HintCallback> hintCallbacks;
	reg.registerBlockType(pluginName, blockTypeName, handler, hints, hintCallbacks);

	// Query the registration:
	auto blockInfo = reg.blockInfo(blockTypeName);
	TEST_NOTEQUAL(blockInfo, nullptr);
	TEST_EQUAL(blockInfo->blockTypeName(), blockTypeName);
	TEST_EQUAL(blockInfo->pluginName(), pluginName);
	TEST_EQUAL(blockInfo->handler(), handler);
	TEST_EQUAL(blockInfo->hintValue(hint1, BlockState()), hint1Value);
	TEST_EQUAL(blockInfo->hintValue("nonexistent", BlockState()), "");
}





/** Tests setting and removing a BlockType hint. */
static void testHintSetRemove()
{
	LOGD("Testing hint addition and removal...");

	// Register the block type:
	BlockTypeRegistry reg;
	AString blockTypeName("test:block1");
	AString pluginName("testPlugin");
	AString hint1("testHint1");
	AString hint1Value("value1");
	AString hint2("testHint2");
	AString hint2Value("value2");
	std::shared_ptr<cBlockHandler> handler(new cBlockHandler(0x12345678));
	std::map<AString, AString> hints = {{hint1, hint1Value}};
	std::map<AString, BlockInfo::HintCallback> hintCallbacks;
	reg.registerBlockType(pluginName, blockTypeName, handler, hints, hintCallbacks);

	// Modify the hints:
	auto blockInfo = reg.blockInfo(blockTypeName);
	reg.setBlockTypeHint(blockTypeName, hint2, hint2Value);
	TEST_EQUAL(blockInfo->hintValue(hint2, BlockState()), hint2Value);  // Was created successfully
	reg.setBlockTypeHint(blockTypeName, hint1, "testValue2");
	TEST_EQUAL(blockInfo->hintValue(hint1, BlockState()), "testValue2");  // Was updated successfully
	reg.removeBlockTypeHint(blockTypeName, hint2);
	TEST_EQUAL(blockInfo->hintValue(hint2, BlockState()), "");  // Was removed successfully

	// Test the error reporting:
	TEST_THROWS(reg.setBlockTypeHint("nonexistent", "hint", "value"), BlockTypeRegistry::NotRegisteredException);
	reg.removeBlockTypeHint(blockTypeName, "nonexistent");  // Should fail silently
}





/** Tests that the plugin-based information is used correctly for registration.
Registers two different block types with two different plugins, then tries to re-register them from a different plugin.
Finally removes the registration through removeAllByPlugin() and checks its success. */
static void testPlugins()
{
	LOGD("Testing plugin-based checks / removal...");

	// Register the block types:
	BlockTypeRegistry reg;
	AString blockTypeName1("test:block1");
	AString pluginName1("testPlugin1");
	AString hint1("testHint1");
	AString hint1Value("value1");
	std::shared_ptr<cBlockHandler> handler1(new cBlockHandler(1));
	std::map<AString, AString> hints = {{hint1, hint1Value}, {"testHint2", "value2"}};
	std::map<AString, BlockInfo::HintCallback> hintCallbacks;
	reg.registerBlockType(pluginName1, blockTypeName1, handler1, hints, hintCallbacks);
	AString blockTypeName2("test:block2");
	AString pluginName2("testPlugin2");
	std::shared_ptr<cBlockHandler> handler2(new cBlockHandler(2));
	reg.registerBlockType(pluginName2, blockTypeName2, handler2, hints, hintCallbacks);

	// Test the refusal to register under a different plugin:
	TEST_THROWS(reg.registerBlockType(pluginName2, blockTypeName1, handler2, hints, hintCallbacks), BlockTypeRegistry::AlreadyRegisteredException);
	TEST_EQUAL(reg.blockInfo(blockTypeName1)->handler()->mIdent, 1);  // Did we overwrite the old registration?
	reg.registerBlockType(pluginName1, blockTypeName1, handler1, hints, hintCallbacks);  // Re-registering must succeed

	// Unregister by plugin, then re-register from a different plugin:
	reg.removeAllByPlugin(pluginName1);
	TEST_EQUAL(reg.blockInfo(blockTypeName1), nullptr);  // Unregistered successfully
	TEST_NOTEQUAL(reg.blockInfo(blockTypeName2), nullptr);  // Didn't unregister from the other plugin
	std::shared_ptr<cBlockHandler> handler3(new cBlockHandler(3));
	reg.registerBlockType(pluginName2, blockTypeName1, handler3, hints, hintCallbacks);
	TEST_NOTEQUAL(reg.blockInfo(blockTypeName1), nullptr);  // Registered successfully
	TEST_EQUAL(reg.blockInfo(blockTypeName1)->pluginName(), pluginName2);
	TEST_EQUAL(reg.blockInfo(blockTypeName1)->handler()->mIdent, 3);
	TEST_EQUAL(reg.blockInfo(blockTypeName2)->handler()->mIdent, 2);
	reg.removeAllByPlugin(pluginName2);
	TEST_EQUAL(reg.blockInfo(blockTypeName1), nullptr);  // Unregistered successfully
	TEST_EQUAL(reg.blockInfo(blockTypeName2), nullptr);  // Unregistered successfully
}




/** Tests that the callback-based hints work properly. */
static void testHintCallbacks()
{
	LOGD("Testing hint callbacks...");

	// Register the block type:
	BlockTypeRegistry reg;
	AString blockTypeName("test:block1");
	AString pluginName("testPlugin");
	AString hint1("testHint1");
	AString hint1Value("value1");
	AString hc1("hintCallback1");
	int callbackCount = 0;
	auto callback1 = [&callbackCount](const AString & aBlockType, const BlockState & aBlockState)
	{
		callbackCount = callbackCount + 1;
		return aBlockType + "_hint";
	};
	std::shared_ptr<cBlockHandler> handler(new cBlockHandler(0x12345678));
	std::map<AString, AString> hints = {{hint1, hint1Value}, {"testHint2", "value2"}};
	std::map<AString, BlockInfo::HintCallback> hintCallbacks = {{hc1, callback1}};
	reg.registerBlockType(pluginName, blockTypeName, handler, hints, hintCallbacks);

	// Check that querying the hint using a callback works:
	TEST_EQUAL(reg.blockInfo(blockTypeName)->hintValue(hc1, BlockState()), blockTypeName + "_hint");
	TEST_EQUAL(callbackCount, 1);  // Called exactly once
}





/** Tests whether thread-locking works properly by running two threads,
one constantly (re-)registering and the other one constantly querying the same block type. */
static void testThreadLocking()
{
	LOGD("Testing thread locking...");

	// Register the block type:
	BlockTypeRegistry reg;
	AString blockTypeName("test:block1");
	AString pluginName("testPlugin");
	AString hint1("testHint1");
	AString hint1Value("value1");
	std::shared_ptr<cBlockHandler> handler(new cBlockHandler(0x12345678));
	std::map<AString, AString> hints = {{hint1, hint1Value}, {"testHint2", "value2"}};
	std::map<AString, BlockInfo::HintCallback> hintCallbacks;
	reg.registerBlockType(pluginName, blockTypeName, handler, hints, hintCallbacks);

	// Run the two threads for at least a second:
	auto endTime = time(nullptr) + 2;
	auto keepRegistering = [&]()
	{
		while (time(nullptr) < endTime)
		{
			reg.registerBlockType(pluginName, blockTypeName, handler, hints, hintCallbacks);
		}
	};
	auto keepQuerying = [&]()
	{
		unsigned numQueries = 0;
		while (time(nullptr) < endTime)
		{
			TEST_NOTEQUAL(reg.blockInfo(blockTypeName), nullptr);
			numQueries += 1;
		}
		LOGD("%u queries have been executed", numQueries);
	};
	std::thread thr1(keepRegistering);
	std::thread thr2(keepQuerying);
	thr1.join();
	thr2.join();
}





static void testBlockTypeRegistry()
{
	testSimpleReg();
	testHintSetRemove();
	testPlugins();
	testHintCallbacks();
	testThreadLocking();
}





IMPLEMENT_TEST_MAIN("BlockTypeRegistryTest",
	testBlockTypeRegistry();
)