-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtest_compute.py
More file actions
533 lines (439 loc) · 18.6 KB
/
test_compute.py
File metadata and controls
533 lines (439 loc) · 18.6 KB
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
# coding:utf-8
"""
In this test we test the code that is usually launched directly from the meshroom_compute script
TODO : We could directly test by launching the executable (`desc.node._MESHROOM_COMPUTE_EXE`)
"""
import os
import re
import shlex
from types import SimpleNamespace
from pathlib import Path
import logging
from meshroom.core.graph import Graph, loadGraph
from meshroom.core import desc, pluginManager, loadClassesNodes
from meshroom.core.node import Status
from meshroom.core.plugins import Plugin
from .utils import registerNodeDesc, unregisterNodeDesc
LOGGER = logging.getLogger("TestCompute")
def executeChunks(node, size):
os.makedirs(node.internalFolder)
logFiles = {}
for chunkIndex in range(size):
iteration = chunkIndex if size > 1 else -1
logFileName = f"{chunkIndex}.log"
logFile = Path(node.internalFolder) / logFileName
logFiles[chunkIndex] = logFile
logFile.touch()
node.prepareLogger(iteration)
node.preprocess()
if size > 1:
chunk = node.chunks[chunkIndex]
chunk.process(True, True)
else:
node.process(True, True)
node.postprocess()
node.restoreLogger()
return logFiles
_INPUTS = [
desc.IntParam(
name="input",
label="Input",
description="input",
value=0,
),
]
_OUTPUTS = [
desc.IntParam(
name="output",
label="Output",
description="Output",
value=None,
),
]
class TestNodeA(desc.BaseNode):
"""
Test process with chunks
"""
__test__ = False
_size = 2
size = desc.StaticNodeSize(2)
parallelization = desc.Parallelization(blockSize=1)
inputs = _INPUTS
outputs = _OUTPUTS
def processChunk(self, chunk):
chunk.logManager.start("info")
iteration = chunk.range.iteration
nbBlocks = chunk.range.nbBlocks
chunk.logger.info(f"> (chunk.logger) {chunk.node.name}")
LOGGER.info(f"> (root logger) {iteration}/{nbBlocks}")
chunk.logManager.end()
class TestNodeB(TestNodeA):
"""
Test process with 1 chunk but still implementing processChunk
"""
__test__ = False
_size = 1
size = desc.StaticNodeSize(1)
parallelization = None
class TestNodeC(desc.BaseNode):
"""
Test process without chunks and without processChunk
"""
__test__ = False
size = desc.StaticNodeSize(1)
parallelization = None
inputs = _INPUTS
outputs = _OUTPUTS
def process(self, node):
LOGGER.info(f"> {node.name}")
class TestNodeLogger:
"""
Test that the logger is correctly set up during the different stages of the compute and that logs are correctly
written in the log file.
"""
logPrefix = r"\[\d{2}:\d{2}:\d{2}\.\d{3}\]\[info\] > "
@classmethod
def setup_class(cls):
registerNodeDesc(TestNodeA)
registerNodeDesc(TestNodeB)
registerNodeDesc(TestNodeC)
@classmethod
def teardown_class(cls):
unregisterNodeDesc(TestNodeA)
unregisterNodeDesc(TestNodeB)
unregisterNodeDesc(TestNodeC)
def test_processChunks(self, tmp_path):
graph = Graph("")
graph._cacheDir = tmp_path
# TestNodeA : multiple chunks
node = graph.addNewNode(TestNodeA.__name__)
# Compute
logFiles = executeChunks(node, 2)
for chunkIndex, logFile in logFiles.items():
with open(logFile, "r") as f:
content = f.read()
reg = re.compile(self.logPrefix + r"\(chunk.logger\) TestNodeA_1")
assert len(reg.findall(content)) == 1
reg = re.compile(self.logPrefix + r"\(root logger\) " + f"{chunkIndex}/2")
assert len(reg.findall(content)) == 1
# TestNodeA : single chunk
nodeB = graph.addNewNode(TestNodeB.__name__)
logFiles = executeChunks(nodeB, 1)
for chunkIndex, logFile in logFiles.items():
with open(logFile, "r") as f:
content = f.read()
reg = re.compile(self.logPrefix + r"\(chunk.logger\) TestNodeB_1")
assert len(reg.findall(content)) == 1
reg = re.compile(self.logPrefix + r"\(root logger\) 0/0")
assert len(reg.findall(content)) == 1
def test_process(self, tmp_path):
graph = Graph("")
graph._cacheDir = tmp_path
node = graph.addNewNode(TestNodeC.__name__)
# Compute
logFiles = executeChunks(node, 1)
for _, logFile in logFiles.items():
with open(logFile, "r") as f:
content = f.read()
reg = re.compile(self.logPrefix + "TestNodeC_1")
assert len(reg.findall(content)) == 1
def test_processChunkInEnvironment_quotesGraphFilepathWithSpaces(self, tmp_path):
graphFilepath = Path(tmp_path, "project with spaces", "scene with spaces.mg")
graphFilepath.parent.mkdir()
nodeDesc = desc.Node()
executed = {}
def executeChunkCommandLine(chunk, cmd, env=None):
executed["cmd"] = cmd
executed["env"] = env
nodeDesc.executeChunkCommandLine = executeChunkCommandLine
plugin = SimpleNamespace(runtimeEnv=None, commandPrefix="", commandSuffix="")
node = SimpleNamespace(
name="TestNode_1",
graph=SimpleNamespace(filepath=graphFilepath.as_posix()),
nodeDesc=SimpleNamespace(pythonExecutable="python", plugin=plugin),
getChunks=lambda: [object(), object()],
)
chunk = SimpleNamespace(node=node, range=SimpleNamespace(iteration=1))
nodeDesc.processChunkInEnvironment(chunk)
assert f'"{graphFilepath.as_posix()}"' in executed["cmd"]
assert shlex.split(executed["cmd"])[2] == graphFilepath.as_posix()
assert "--iteration 1" in executed["cmd"]
class TestLockUpdates:
"""
Tests for node locking behaviour during status transitions. Nodes should be properly locked when they undergo
computation statuses and unlocked when their status is reset (through parameter changes, for example).
"""
plugin = None
@classmethod
def setup_class(cls):
folder = os.path.join(os.path.dirname(__file__), "plugins", "meshroom")
package = "pluginA"
cls.plugin = Plugin(package, folder)
nodes = loadClassesNodes(folder, package)
for node in nodes:
cls.plugin.addNodePlugin(node)
pluginManager.addPlugin(cls.plugin)
@classmethod
def teardown_class(cls):
for node in cls.plugin.nodes.values():
pluginManager.unregisterNode(node)
pluginManager.removePlugin(cls.plugin)
cls.plugin = None
@staticmethod
def checkNodeStatusAndLock(node, expectedStatus, expectedLock):
assert node.globalStatus == expectedStatus.name
assert node.locked == expectedLock
def test_lockDuringComputation(self, graphSavedOnDisk):
"""
Test that a node is properly locked during the execution of its "process()" method and unlocked once the process
is finished. Both the global status and the lock status should be updated throughout the process.
"""
import threading
import time
graph: Graph = graphSavedOnDisk
node = graph.addNewNode("PluginANodeA")
graph.save()
self.checkNodeStatusAndLock(node, Status.NONE, False)
# PluginANodeA will sleep 3 seconds in its "process", so we can check the status and lock during the process execution
thread = threading.Thread(target=node.process, kwargs={"inCurrentEnv": True})
thread.start()
time.sleep(0.5) # Wait for the process to start and update the status
self.checkNodeStatusAndLock(node, Status.RUNNING, True)
# Wait for the process to finish and update the status
thread.join()
self.checkNodeStatusAndLock(node, Status.SUCCESS, False)
def test_lockResetOnParameterChange(self, graphSavedOnDisk):
"""
Test that a node's lock is properly reset when its status is reset,
for example through parameter changes.
"""
graph: Graph = graphSavedOnDisk
node = graph.addNewNode("PluginANodeA")
graph.save()
self.checkNodeStatusAndLock(node, Status.NONE, False)
node.process(inCurrentEnv=True)
self.checkNodeStatusAndLock(node, Status.SUCCESS, False)
# Change a parameter to reset the status and check that the lock is also reset
node.input.value = "path"
self.checkNodeStatusAndLock(node, Status.NONE, False)
def test_lockResetOnDuplicatedParameterChange(self, graphSavedOnDisk):
"""
Test that when a node is duplicated while running, the duplicate node is independent from the original one
and that changing a parameter on the duplicate node resets its status and lock without impacting the original
node's status and lock.
"""
import threading
import time
graph: Graph = graphSavedOnDisk
node = graph.addNewNode("PluginANodeA")
graph.save()
self.checkNodeStatusAndLock(node, Status.NONE, False)
thread = threading.Thread(target=node.process, kwargs={"inCurrentEnv": True})
thread.start()
time.sleep(0.5)
self.checkNodeStatusAndLock(node, Status.RUNNING, True)
# Duplicate the running & locked node
duplicate = graph.duplicateNodes([node])
# "duplicate" is an ordered_dict with the original node as key and a list of duplicates as value.
# We know there is only one duplicate in this test.
assert len(duplicate) == 1
duplicate = list(duplicate.values())[0][0]
# Check the duplicate node is valid
assert duplicate is not None
assert duplicate.nodeType == node.nodeType
assert duplicate.name != node.name
# Check the status of the duplicate node is RUNNING but that it is not locked:
# it has not been computed and should be independent from the original node
self.checkNodeStatusAndLock(duplicate, Status.RUNNING, False)
# Change a parameter to reset the duplicatenode's status and check that the lock is also reset
duplicate.input.value = "path"
self.checkNodeStatusAndLock(duplicate, Status.NONE, False)
self.checkNodeStatusAndLock(node, Status.RUNNING, True)
thread.join()
self.checkNodeStatusAndLock(node, Status.SUCCESS, False)
self.checkNodeStatusAndLock(duplicate, Status.NONE, False)
def test_noLockResetOnGraphLoad(self, graphSavedOnDisk):
"""
Test that when a graph is loaded while a node is running, the node's status and lock are not reset and that
the node is still locked. """
import threading
import time
graph: Graph = graphSavedOnDisk
node = graph.addNewNode("PluginANodeA")
graph.save()
self.checkNodeStatusAndLock(node, Status.NONE, False)
thread = threading.Thread(target=node.process, kwargs={"inCurrentEnv": True})
thread.start()
time.sleep(0.5)
self.checkNodeStatusAndLock(node, Status.RUNNING, True)
# Load the graph while the node is running and check that the node's status and lock are not reset
loadedGraph = loadGraph(graph.filepath)
loadedNode = loadedGraph.node(node.name)
self.checkNodeStatusAndLock(loadedNode, Status.RUNNING, True)
thread.join()
# Make sure the status is up-to-date
loadedNode.updateStatusFromCache()
self.checkNodeStatusAndLock(loadedNode, Status.SUCCESS, False)
def test_noDownstreamNodeLockDuringComputation(self, graphSavedOnDisk):
"""
Test that when a node is running, its downstream nodes are not locked, and their status is not updated.
"""
import threading
import time
graph: Graph = graphSavedOnDisk
node = graph.addNewNode("PluginANodeA")
downstreamNode = graph.addNewNode("PluginANodeB")
node.output.connectTo(downstreamNode.input)
graph.save()
self.checkNodeStatusAndLock(node, Status.NONE, False)
self.checkNodeStatusAndLock(downstreamNode, Status.NONE, False)
thread = threading.Thread(target=node.process, kwargs={"inCurrentEnv": True})
thread.start()
time.sleep(0.5)
self.checkNodeStatusAndLock(node, Status.RUNNING, True)
self.checkNodeStatusAndLock(downstreamNode, Status.NONE, False)
thread.join()
self.checkNodeStatusAndLock(node, Status.SUCCESS, False)
self.checkNodeStatusAndLock(downstreamNode, Status.NONE, False)
def test_upstreamLockDuringComputation(self, graphSavedOnDisk):
"""
Test that when a node is running, its upstream nodes are locked and their status remains unchanged.
"""
import threading
import time
graph: Graph = graphSavedOnDisk
node = graph.addNewNode("PluginANodeA")
downstreamNode = graph.addNewNode("PluginANodeB")
node.output.connectTo(downstreamNode.input)
graph.save()
self.checkNodeStatusAndLock(node, Status.NONE, False)
self.checkNodeStatusAndLock(downstreamNode, Status.NONE, False)
node.process(inCurrentEnv=True)
self.checkNodeStatusAndLock(node, Status.SUCCESS, False)
self.checkNodeStatusAndLock(downstreamNode, Status.NONE, False)
thread = threading.Thread(target=downstreamNode.process, kwargs={"inCurrentEnv": True})
thread.start()
time.sleep(0.5)
self.checkNodeStatusAndLock(node, Status.SUCCESS, True)
self.checkNodeStatusAndLock(downstreamNode, Status.RUNNING, True)
thread.join()
self.checkNodeStatusAndLock(node, Status.SUCCESS, False)
self.checkNodeStatusAndLock(downstreamNode, Status.SUCCESS, False)
def test_noDownstreamLockAfterParameterChange(self, graphSavedOnDisk):
"""
Test that when a computed node's parameter is updated, the downstream node's status and lock are
updated accordingly.
"""
graph: Graph = graphSavedOnDisk
node = graph.addNewNode("PluginANodeA")
downstreamNode = graph.addNewNode("PluginANodeB")
node.output.connectTo(downstreamNode.input)
graph.save()
self.checkNodeStatusAndLock(node, Status.NONE, False)
self.checkNodeStatusAndLock(downstreamNode, Status.NONE, False)
node.process(inCurrentEnv=True)
downstreamNode.process(inCurrentEnv=True)
self.checkNodeStatusAndLock(node, Status.SUCCESS, False)
self.checkNodeStatusAndLock(downstreamNode, Status.SUCCESS, False)
# Change a parameter on the upstream node and check that the downstream node's status is reset but not locked
node.input.value = "path"
self.checkNodeStatusAndLock(node, Status.NONE, False)
self.checkNodeStatusAndLock(downstreamNode, Status.NONE, False)
def test_noUpstreamLockAfterParameterChange(self, graphSavedOnDisk):
"""
Test that when a computed node's parameter is updated, the upstream node's status and lock are not
impacted.
"""
graph: Graph = graphSavedOnDisk
node = graph.addNewNode("PluginANodeA")
downstreamNode = graph.addNewNode("PluginANodeB")
node.output.connectTo(downstreamNode.input)
graph.save()
self.checkNodeStatusAndLock(node, Status.NONE, False)
self.checkNodeStatusAndLock(downstreamNode, Status.NONE, False)
node.process(inCurrentEnv=True)
downstreamNode.process(inCurrentEnv=True)
self.checkNodeStatusAndLock(node, Status.SUCCESS, False)
self.checkNodeStatusAndLock(downstreamNode, Status.SUCCESS, False)
# Disconnect the downstream node and check that the upstream node's status is not reset and that it is not locked
downstreamNode.input.disconnectEdge()
self.checkNodeStatusAndLock(node, Status.SUCCESS, False)
self.checkNodeStatusAndLock(downstreamNode, Status.NONE, False)
class TestNode_SizeA(desc.BaseNode):
__test__ = False
size = desc.DynamicNodeSize("nbChunks")
parallelization = desc.Parallelization(blockSize=1)
inputs = [
desc.IntParam(
name="nbChunks",
label="nbChunks",
description="number of chunks",
value=2,
),
desc.File(
name="nodeInput",
label="Node Input",
description="",
value="",
),
]
outputs = [
desc.File(
name='output',
label='Output',
description='Output',
value=os.path.join("{nodeCacheFolder}"),
commandLineGroup='',
),
]
def processChunk(self, chunk):
pass
class TestNode_SizeB(TestNode_SizeA):
""" Inherit the linked node size but not parallelized """
size = desc.DynamicNodeSize("nodeInput")
parallelization = False
class TestNode_SizeC(TestNode_SizeA):
""" Inherit the linked node size and parallelized """
size = desc.DynamicNodeSize("nodeInput")
parallelization = desc.Parallelization(blockSize=1)
class TestSizeUpdate:
plugin = None
@classmethod
def setup_class(cls):
registerNodeDesc(TestNode_SizeA)
registerNodeDesc(TestNode_SizeB)
registerNodeDesc(TestNode_SizeC)
@classmethod
def teardown_class(cls):
unregisterNodeDesc(TestNode_SizeA)
unregisterNodeDesc(TestNode_SizeB)
unregisterNodeDesc(TestNode_SizeC)
@staticmethod
def checkNodeSizeAndStatus(node, nodeSize, nbChunks, status):
assert node.size == nodeSize
assert len(node._chunks) == nbChunks
assert node.globalStatus == status.name
def test_correctSizeUpdate(self, graphSavedOnDisk):
graph: Graph = graphSavedOnDisk
nodeA = graph.addNewNode("TestNode_SizeA")
nodeB = graph.addNewNode("TestNode_SizeB")
nodeA.output.connectTo(nodeB.nodeInput)
nodeC = graph.addNewNode("TestNode_SizeC")
nodeB.output.connectTo(nodeC.nodeInput)
graph.save()
# A
self.checkNodeSizeAndStatus(nodeA, 0, 0, Status.NONE)
nodeA.createChunks()
nodeA.process(inCurrentEnv=True)
self.checkNodeSizeAndStatus(nodeA, 2, 2, Status.SUCCESS)
# B
self.checkNodeSizeAndStatus(nodeB, 0, 1, Status.NONE)
nodeB.createChunks()
nodeB._updateNodeSize()
nodeB.process(inCurrentEnv=True)
self.checkNodeSizeAndStatus(nodeB, 2, 1, Status.SUCCESS)
# C
self.checkNodeSizeAndStatus(nodeC, 0, 0, Status.NONE)
nodeC.createChunks()
nodeC.process(inCurrentEnv=True)
self.checkNodeSizeAndStatus(nodeC, 2, 2, Status.SUCCESS)