from counter import RiakCounter
import logging
import riak

logging.basicConfig(level=logging.DEBUG)

# Create 3 independent counters
c1 = RiakCounter(riak.RiakClient(port=8098).bucket("test"), "counter")
c2 = RiakCounter(riak.RiakClient(port=8098).bucket("test"), "counter")
c3 = RiakCounter(riak.RiakClient(port=8098).bucket("test"), "counter")

# Counter 1 will increment the counter to three
c1.reset()   # 0
c1.add(1) # 1
c1.add(1) # 2
c1.add(1) # 3
assert c1.value == 3, c1.value

# Counter 2 and Counter 3 will increment the counter in parallel
# Split reality by fetching two copies of the some object
o2 = c2.bucket.get(c2.key)
o3 = c3.bucket.get(c3.key)
# Attempt to update the values using that copy, this should break the
# space/time continuum.
c2.add(1, obj=o2)  # 4
c3.add(10, obj=o3) # 14 

# c1 should get a conflicted value and the conflict should be resolved with the value returned.
assert c1.value == 14, c1.value