1#!/usr/bin/env python
2
3import sys
4import time
5import exceptions
6
7sys.path.append('../management')
8import mc_bin_client
9
10EXPIRY = 2
11VERBOSE = False
12
13def log(*s):
14    if VERBOSE:
15        print ' '.join(s)
16
17def store(mc, kprefix):
18    k = kprefix + '.set'
19    log(">> ", k)
20    mc.set(k, EXPIRY, 0, k)
21    k = kprefix + '.add'
22    log("++ ", k)
23    mc.add(k, EXPIRY, 0, k)
24
25def check(mc, kprefix):
26    for suffix in ['.set', '.add']:
27        try:
28            k = kprefix + suffix
29            log("<< ", k)
30            mc.get(k)
31            raise exceptions.Exception("Expected to fail to get " + k)
32        except mc_bin_client.MemcachedError:
33            pass
34
35if __name__ == '__main__':
36    mc = mc_bin_client.MemcachedClient()
37
38    if '-v' in sys.argv:
39        VERBOSE = True
40
41    store(mc, 'a1')
42    store(mc, 'a2')
43    log("(sleep)")
44    time.sleep(EXPIRY + 1)
45    check(mc, 'a2')
46    store(mc, 'a1')
47    store(mc, 'a2')
48