blob: 1af0e839a34307b43ee1c034a91235b54bef921c (
plain)
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
|
#!/usr/bin/env python
import os
import rdflib
import unittest
import pytest
try:
from os import fork
from os import pipe
except ImportError:
pytestmark = pytest.mark.skip(
reason="No os.fork() and/or os.pipe() on this platform, skipping"
)
class TestRandomSeedInFork(unittest.TestCase):
def test_bnode_id_differs_in_fork(self):
"""Checks that os.fork()ed child processes produce a
different sequence of BNode ids from the parent process.
"""
r, w = os.pipe() # these are file descriptors, not file objects
pid = os.fork()
if pid:
pb1 = rdflib.term.BNode()
os.close(w) # use os.close() to close a file descriptor
r = os.fdopen(r) # turn r into a file object
txt = r.read()
os.waitpid(pid, 0) # make sure the child process gets cleaned up
r.close()
else:
os.close(r)
w = os.fdopen(w, "w")
cb = rdflib.term.BNode()
w.write(cb)
w.close()
os._exit(0)
assert txt != str(
pb1
), "Parent process BNode id: " + "%s, child process BNode id: %s" % (
txt,
str(pb1),
)
if __name__ == "__main__":
unittest.main()
|