from lib.memcached import Memcached

server.memcached.execute("cas bad blah 0 0 0\r\n", silent=False)
server.memcached.execute("cas bad 0 blah 0 0\r\n", silent=False)
server.memcached.execute("cas bad 0 0 blah 0\r\n", silent=False)
server.memcached.execute("cas bad 0 0 0 blah\r\n", silent=False)

print """# gets foo (should not exist) """
server.memcached.execute("gets foo", silent=False)

print """# set foo """
server.memcached.execute("set foo 0 0 6\r\nbarval", silent=False)

print """# gets foo and verify identifier exists """
server.memcached.execute("gets foo", silent=False)

print """# cas fail """
server.memcached.execute("cas foo 0 0 6 123\r\nbarva2", silent=False)

print """# gets foo and verify identifier exists """
result = server.memcached.execute("gets foo", silent=False)
identifier = int(result.split()[4])

print """# cas success """
server.memcached.execute("cas foo 0 0 6 %d\r\nbarva2" % identifier, silent=False)

print """# cas failure (reusing the same key) """
server.memcached.execute("cas foo 0 0 6 %d\r\nbarva2" % identifier, silent=False)

print """# delete foo """
server.memcached.execute("delete foo\r\n", silent=False);

print """# cas missing """
server.memcached.execute("cas foo 0 0 6 %d\r\nbarva2" % identifier, silent=False)

print """# set foo1 """
server.memcached.execute("set foo1 0 0 1\r\n1", silent=False)

print """# set foo2 """
server.memcached.execute("set foo2 0 0 1\r\n2", silent=False)

print """# gets foo1 check """
result = server.memcached.execute("gets foo1", silent=False)
foo1_cas = int(result.split()[4])

print """# gets foo2 check """
result = server.memcached.execute("gets foo2", silent=False)
foo2_cas = int(result.split()[4])

print """# validate foo1 != foo2 """
if foo1_cas != foo2_cas:
    print "success: foo1_cas (%d) != foo2_cas (%d)" % (foo1_cas, foo2_cas)
else:
    print "fail: foo1_cas (%d) == foo2_cas (%d)" % (foo1_cas, foo2_cas)

memcached1 = server.memcached
memcached2 = Memcached('localhost', server.memcached_port)

print """# gets foo from memcached1 - should success """
result = memcached1.execute("gets foo1", silent = False)
mem1_cas = int(result.split()[4])

print """# gets foo from memcached2 - should success """
result = memcached2.execute("gets foo1", silent = False)
mem2_cas = int(result.split()[4])

print """# send 'cas foo1' from memcached1 """
memcached1.sendCommands("cas foo1 0 0 6 %d\r\nbarva2" % mem1_cas, silent = False)
print """# send 'cas foo1' from memcached2 """
memcached2.sendCommands("cas foo1 0 0 4 %d\r\npear" % mem2_cas, silent = False)

print """# recv reply 'cas foo1' from memcached1 """
result = memcached1.recvReply(silent = True)
mem1_cas_result = result.split()[0]
print """# recv reply 'cas foo1' from memcached2 """
result = memcached2.recvReply(silent = True)
mem2_cas_result = result.split()[0]

if mem1_cas_result == "STORED" and mem2_cas_result == "EXISTS":
    print "race cas: pass"
elif mem1_cas_result == "EXISTS" and mem2_cas_result == "STORED":
    print "race cas: pass"
else:
    print "race cas: fail"
    print "cas 1 = %s" % mem1_cas_result
    print "cas 2 = %s" % mem2_cas_result

print """# set bug15 """
server.memcached.execute("set bug15 0 0 1\r\n0", silent=False)

print """# Check out the first gets. """
result = server.memcached.execute("gets bug15", silent=True)
bug15_cas = int(result.split()[4])

print """# Increment. """
server.memcached.execute("incr bug15 1", silent=False)

print """# Validate a changed CAS. """
result = server.memcached.execute("gets bug15", silent=True)
next_bug15_cas = int(result.split()[4])

print """# validate bug15_cas != next_bug15_cas """
if bug15_cas != next_bug15_cas:
    print "success: bug15_cas != next_bug15_cas"
else:
    print "fail: bug15_cas == next_bug15_cas"

# resore default suite config
server.stop()
server.deploy(self.suite_ini["config"])
server.start()
# vim: syntax=python
