Skip to content

Commit 1e352ab

Browse files
Add check that no database has been loaded
1 parent b5f5e0a commit 1e352ab

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

softioc/builder.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,6 @@ def longStringOut(name, **fields):
274274
# ----------------------------------------------------------------------------
275275
# Support routines for builder
276276

277-
278-
_DatabaseWritten = False
279-
280277
def LoadDatabase():
281278
'''This should be called after all the builder records have been created,
282279
but before calling iocInit(). The database is loaded into EPICS memory,
@@ -293,6 +290,8 @@ def LoadDatabase():
293290

294291
def ClearRecords():
295292
"""Delete all created record information, allowing new record creation"""
293+
assert not pythonSoftIoc.RecordWrapper.is_builder_reset(), \
294+
'Record database has already been loaded'
296295
RecordLookup._RecordDirectory.clear()
297296
ResetRecords()
298297

softioc/pythonSoftIoc.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class RecordWrapper(object):
1717
__builder_reset = False
1818

1919
def __init__(self, builder, device, name, **fields):
20-
assert not self.__builder_reset, \
20+
assert not self.is_builder_reset(), \
2121
'It\'s too late to create records'
2222
# List of keyword arguments expected by the device constructor. The
2323
# remaining arguments are passed to the builder. It's a shame we
@@ -47,6 +47,12 @@ def reset_builder(cls):
4747
for instance in cls.__Instances:
4848
instance.__set('__builder', None)
4949

50+
@classmethod
51+
def is_builder_reset(cls):
52+
'''Returns True if it is too late to create records'''
53+
return cls.__builder_reset
54+
55+
5056

5157
# Most attributes delegate directly to the builder instance until the
5258
# database has been written. At this point the builder instance has been

tests/test_records.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,39 @@ def test_clear_records():
255255
for key, val in LookupRecordList():
256256
assert key == full_name
257257

258+
def clear_records_runner(child_conn):
259+
"""Tests ClearRecords after loading the database"""
260+
builder.aOut("Some-Record")
261+
builder.LoadDatabase()
262+
263+
try:
264+
builder.ClearRecords()
265+
except AssertionError:
266+
# Expected error
267+
child_conn.send("D") # "Done"
268+
child_conn.send("F") # "Fail"
269+
270+
def test_clear_records_too_late():
271+
"""Test that calling ClearRecords after a database has been loaded raises
272+
an exception"""
273+
ctx = get_multiprocessing_context()
274+
275+
parent_conn, child_conn = ctx.Pipe()
276+
277+
process = ctx.Process(
278+
target=clear_records_runner,
279+
args=(child_conn,),
280+
)
281+
282+
process.start()
283+
284+
try:
285+
select_and_recv(parent_conn, "D")
286+
finally:
287+
process.join(timeout=TIMEOUT)
288+
if process.exitcode is None:
289+
pytest.fail("Process did not terminate")
290+
258291

259292

260293
def validate_fixture_names(params):

0 commit comments

Comments
 (0)