-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_example.py
68 lines (53 loc) · 2.29 KB
/
test_example.py
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
import faultguard
import numpy as np
import os
def launch(faultguard_data, args):
"""
Demo software main method
:param faultguard_data: Faultguard data dictionary
:param args: Data passed from faultguard.start.
"""
print("Launching demo")
# Some important data
important_data_1 = np.array([1,2,3])
important_data_2 = args[0] + " " + args[1]
# Some dummy important data manipulation
for i in range(10):
important_data_1[i%3] = i
important_data_2 += str(i)
print("important_data_1:", important_data_1)
print("important_data_2:", important_data_2)
# Sending important data to faultguard process
faultguard_data["important_data_1"] = important_data_1
faultguard_data["important_data_2"] = important_data_2
# Generate segfault
if i == 7:
import ctypes
ctypes.string_at(0)
def rescue(faultguard_data, exit_code, args):
"""
Demo rescue handler
:param faultguard_data: Faultguard data dictionary
:param exit_code: Exit code of occured fault.
:param args: Data passed from faultguard.start.
"""
print("Fault occured. Exit code: {}. Rescued data:".format(exit_code))
# Check if fault occurs before data was initialized
if "important_data_1" not in faultguard_data or "important_data_2" not in faultguard_data:
raise RuntimeError("Faultguard dict content missing")
# Restore data
important_data_1 = faultguard_data["important_data_1"]
important_data_2 = faultguard_data["important_data_2"]
# You might need to assign the class here by important_data_1.__class__ = ...
print("important_data_1:", important_data_1)
print("important_data_2:", important_data_2)
assert np.all(important_data_1 == [6, 7, 5])
assert important_data_2 == "Hello World01234567"
def test_main_autosave():
if os.path.isfile("test2.tmp.xz"):
os.remove("test2.tmp.xz")
faultguard.start(launch, rescue, args=("Hello", "World"), autosave_interval=3, autosave_file="test2.tmp.xz")
assert not os.path.isfile("test2.tmp.xz")
assert not os.path.isfile("test2.tmp.xz.tmp")
def test_main_no_autosave():
faultguard.start(launch, rescue, args=("Hello", "World"))