@@ -39,6 +39,99 @@ def test_hit(self):
39
39
40
40
self .assertEquals (self .successResultOf (cache .get ("foo" )), 123 )
41
41
42
+ def test_hit_deferred (self ):
43
+ cache = DeferredCache ("test" )
44
+ origin_d = defer .Deferred ()
45
+ set_d = cache .set ("k1" , origin_d )
46
+
47
+ # get should return an incomplete deferred
48
+ get_d = cache .get ("k1" )
49
+ self .assertFalse (get_d .called )
50
+
51
+ # add a callback that will make sure that the set_d gets called before the get_d
52
+ def check1 (r ):
53
+ self .assertTrue (set_d .called )
54
+ return r
55
+
56
+ get_d .addCallback (check1 )
57
+
58
+ # now fire off all the deferreds
59
+ origin_d .callback (99 )
60
+ self .assertEqual (self .successResultOf (origin_d ), 99 )
61
+ self .assertEqual (self .successResultOf (set_d ), 99 )
62
+ self .assertEqual (self .successResultOf (get_d ), 99 )
63
+
64
+ def test_callbacks (self ):
65
+ """Invalidation callbacks are called at the right time"""
66
+ cache = DeferredCache ("test" )
67
+ callbacks = set ()
68
+
69
+ # start with an entry, with a callback
70
+ cache .prefill ("k1" , 10 , callback = lambda : callbacks .add ("prefill" ))
71
+
72
+ # now replace that entry with a pending result
73
+ origin_d = defer .Deferred ()
74
+ set_d = cache .set ("k1" , origin_d , callback = lambda : callbacks .add ("set" ))
75
+
76
+ # ... and also make a get request
77
+ get_d = cache .get ("k1" , callback = lambda : callbacks .add ("get" ))
78
+
79
+ # we don't expect the invalidation callback for the original value to have
80
+ # been called yet, even though get() will now return a different result.
81
+ # I'm not sure if that is by design or not.
82
+ self .assertSetEqual (callbacks , set ())
83
+
84
+ # now fire off all the deferreds
85
+ origin_d .callback (20 )
86
+ self .assertEqual (self .successResultOf (set_d ), 20 )
87
+ self .assertEqual (self .successResultOf (get_d ), 20 )
88
+
89
+ # now the original invalidation callback should have been called, but none of
90
+ # the others
91
+ self .assertSetEqual (callbacks , {"prefill" })
92
+ callbacks .clear ()
93
+
94
+ # another update should invalidate both the previous results
95
+ cache .prefill ("k1" , 30 )
96
+ self .assertSetEqual (callbacks , {"set" , "get" })
97
+
98
+ def test_set_fail (self ):
99
+ cache = DeferredCache ("test" )
100
+ callbacks = set ()
101
+
102
+ # start with an entry, with a callback
103
+ cache .prefill ("k1" , 10 , callback = lambda : callbacks .add ("prefill" ))
104
+
105
+ # now replace that entry with a pending result
106
+ origin_d = defer .Deferred ()
107
+ set_d = cache .set ("k1" , origin_d , callback = lambda : callbacks .add ("set" ))
108
+
109
+ # ... and also make a get request
110
+ get_d = cache .get ("k1" , callback = lambda : callbacks .add ("get" ))
111
+
112
+ # none of the callbacks should have been called yet
113
+ self .assertSetEqual (callbacks , set ())
114
+
115
+ # oh noes! fails!
116
+ e = Exception ("oops" )
117
+ origin_d .errback (e )
118
+ self .assertIs (self .failureResultOf (set_d , Exception ).value , e )
119
+ self .assertIs (self .failureResultOf (get_d , Exception ).value , e )
120
+
121
+ # the callbacks for the failed requests should have been called.
122
+ # I'm not sure if this is deliberate or not.
123
+ self .assertSetEqual (callbacks , {"get" , "set" })
124
+ callbacks .clear ()
125
+
126
+ # the old value should still be returned now?
127
+ get_d2 = cache .get ("k1" , callback = lambda : callbacks .add ("get2" ))
128
+ self .assertEqual (self .successResultOf (get_d2 ), 10 )
129
+
130
+ # replacing the value now should run the callbacks for those requests
131
+ # which got the original result
132
+ cache .prefill ("k1" , 30 )
133
+ self .assertSetEqual (callbacks , {"prefill" , "get2" })
134
+
42
135
def test_get_immediate (self ):
43
136
cache = DeferredCache ("test" )
44
137
d1 = defer .Deferred ()
0 commit comments