Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #56, undo RepyV1 uniqueid patch #194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions tests/ut_seattlelib_uniqueidbasictest.r2py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Check that uniqueid returns unique ids, also if imported multiple times,
and across multiple threads.
"""
#pragma repy restrictions.default dylink.r2py

uniqueid = dy_import_module("uniqueid.r2py")


# Test 1: Get a couple of IDs and see if they are indeed unique.

oldids = []

for i in range(100):
newid = uniqueid.uniqueid_getid()
assert newid not in oldids, "Saw the same ID twice: " + str(oldid)
oldids.append(newid)



# Test 2: "Reimport" the library. (Dylink should yield the existing instance.)

uniqueid2 = dy_import_module("uniqueid.r2py")

assert uniqueid2.uniqueid_getid() != uniqueid.uniqueid_getid(), "Got the same ID twice after pseudo-reimporting!"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also verify that uniqueid2 does not return an id that was already returned by uniqueid, i.e.
assert uniqueid2.uniqueid_getid() not in oldids to catch the case that the new import resets the module store.




# Test 3: Get IDs in threads

idlist = []

def get_an_id():
idlist.append(uniqueid.uniqueid_getid())

for i in range(10):
createthread(get_an_id)

# Give the threads some time to spawn and finish
sleep(5)

for anid in idlist:
assert idlist.count(anid)==1, "Duplicate ID: " + str(anid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idlist.count(anid) == 1 (whitespaces)


16 changes: 3 additions & 13 deletions uniqueid.r2py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,12 @@ Start date: November 11th, 2008

This is a really, really simple module, only broken out to avoid duplicating
functionality.

NOTE: This will give unique ids PER FILE. If you have multiple python
modules that include this, they will have the potential to generate the
same ID.

"""

# This is a list to prevent using part of the user's mycontext dict
# We use getruntime() instead of a list starting with 0, as this
# library may get imported multiple times.
# See ticket #1319 and #1318 for more details.
# Use this list as module-level storage. (This avoids cluttering the
# programmer's `mycontext` dict.)

current_time = getruntime()
uniqueid_idlist = [int((current_time - int(current_time)) * 2**32)]
uniqueid_idlist = [0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why exactly does this have to be a list? It will always only hold one element, right?


uniqueid_idlock = createlock()

Expand All @@ -44,8 +36,6 @@ def uniqueid_getid():

uniqueid_idlock.acquire(True)

# I'm using a list because I need a global, but don't want to use the
# programmer's dict
myid = uniqueid_idlist[0]
uniqueid_idlist[0] = uniqueid_idlist[0] + 1

Expand Down