Skip to content

Instantly share code, notes, and snippets.

@ketralnis
Created October 8, 2010 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ketralnis/617399 to your computer and use it in GitHub Desktop.
Save ketralnis/617399 to your computer and use it in GitHub Desktop.
redditcastest.py
from r2.models import *
from r2.lib.utils import thread_dump
def cc():
g.cache.caches[0].clear()
g.cache.caches[0].flush_all()
cc()
lid = 42
# _commit set thing
l = Link._byID(42)
# set 0
l._ups = 0
l._commit()
assert l._ups == 0, "Initial set _ups to 0"
g.cache.caches[0].clear()
l = Link._byID(42)
assert l._ups == 0, "memcached set _ups to 0"
cc()
l = Link._byID(42)
assert l._ups == 0, "postgres set _ups to 0"
l._ups = 42
l._commit()
assert l._ups == 42, "localobj set _ups to 42"
g.cache.caches[0].clear()
l = Link._byID(42)
assert l._ups == 42, "memcached set _ups to 42"
cc()
l = Link._byID(42)
assert l._ups == 42, "postgres set _ups to 42"
# _commit set new data
cc()
l = Link()
l.foo = 'True'
l._commit()
print 'creating %r' % (l,)
assert l.foo == 'True', "Initial set foo to 'True'"
g.cache.caches[0].clear()
l = Link._byID(l._id)
assert l.foo == 'True', "memcached set foo to 'True'"
cc()
l = Link._byID(l._id)
assert l.foo == 'True', "postgres set foo to 'True'"
# _commit set existing data
l.foo = 'False'
l._commit()
assert l._t['foo'] == 'False', "_t should have 'foo' == 'False'"
assert l.foo == 'False', "localobj set foo to 'False'"
g.cache.caches[0].clear()
l = Link._byID(l._id)
assert l.foo == 'False', "memcached set foo to 'False'"
cc()
l = Link._byID(l._id)
assert l.foo == 'False', "postgres set foo to 'False'"
# _incr thing
wasups = l._ups
l._incr('_ups')
assert l._ups == wasups+1, 'localobj incr _ups'
g.cache.caches[0].clear()
l = Link._byID(l._id)
assert l._ups == wasups+1, 'memcached incr _ups'
cc()
l = Link._byID(l._id)
assert l._ups == wasups+1, 'postgres incr _ups'
# _incr data from default
assert 'reported' in Link._defaults and 'reported' not in l._t, "This test needs an integer property with a default"
l._incr('reported')
assert l.reported == 1, 'localobj reports from default'
g.cache.caches[0].clear()
l = Link._byID(l._id)
assert l.reported == 1, 'memcached incr reports from default'
cc()
l = Link._byID(l._id)
assert l.reported == 1, 'postgres incr reports from default'
# _incr existing data
assert l.reported == 1, "This test needs a non-default int prop ready for incr"
l._incr('reported')
assert l.reported == 2, 'localobj reports from existing'
g.cache.caches[0].clear()
l = Link._byID(l._id)
assert l.reported == 2, 'memcached incr reports from existing'
cc()
l = Link._byID(l._id)
assert l.reported == 2, 'postgres incr reports from existing'
###############################################
from threading import Thread, current_thread
import time
start = time.time()
print 'starting cas thread test at', start
num_threads=5
thread_incrs=1000
memc = g.cache.caches[1]
key='thekey'
memc.delete(key)
assert memc.num_clients >= num_threads, "We need g.num_mc_clients(%d) >= num_threads(%d) for this test" % (g.num_mc_clients, num_threads)
threads = []
def nffn():
return 0
def incr_it(x):
return x+1
def thread_fn_cas():
for y in xrange(thread_incrs):
memc.mutate(key, incr_it, nffn=nffn)
for x in range(num_threads):
t = Thread(target=thread_fn_cas,name='cas increrer %d' % x)
t.daemon=True
threads.append(t)
t.start()
for x in threads:
x.join(30)
if x.isAlive():
thread_dump()
raise Exception("%r refused to die" % (x.name,))
answer = memc.get(key)
assert answer == num_threads*thread_incrs, 'Wrong answer: %d (did one of my children die?)' % (answer,)
print 'finished cas thread test in', time.time()-start
start=time.time()
print 'starting lock thread test at', start
memc.delete(key)
make_lock = g.make_lock
threads = []
def thread_fn_lock():
memc.add(key, 0)
for y in xrange(thread_incrs):
with make_lock('lock_%s' % key):
old = memc.get(key)
memc.set(key, old+1)
for x in range(num_threads):
t = Thread(target=thread_fn_lock,name='lock increrer %d' % x)
t.daemon=True
threads.append(t)
t.start()
for x in threads:
x.join(30)
if x.isAlive():
thread_dump()
raise Exception("%r refused to die" % (x.name,))
answer = memc.get(key)
assert answer == num_threads*thread_incrs, 'Wrong answer: %d (did one of my children die?)' % (answer,)
print 'finished lock thread test in', time.time()-start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment