My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Copyright (c) 2009 Jens Scheffler (appenginefan.com)
*
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the
* License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in
* writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing
* permissions and limitations under the License.
*/

package com.appenginefan.toolkit.common;

import java.util.Random;

import org.easymock.classextension.EasyMock;

import com.appenginefan.toolkit.persistence.DatastorePersistence;
import com.appenginefan.toolkit.persistence.LongPersistence;
import com.appenginefan.toolkit.persistence.MapBasedPersistence;
import com.appenginefan.toolkit.persistence.Persistence;
import com.appenginefan.toolkit.unittests.BaseTest;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.google.appengine.api.memcache.MemcacheService.SetPolicy;
import com.google.common.base.Functions;

/**
* Unit tests for the Counter class
*/
public class CounterTest
extends BaseTest {

private static final String MEMCACHE_KEY = "aef/c/tst";

private static final String STORE_PREFIX = "/tst/";

private static final double CHANCE = 0.5;

private static final int NUM_SHARDS = 10;

private MemcacheService memcache;

private Persistence<Long> datastore;

private Random random;

private Counter counter;

@Override
protected void setUp() throws Exception {
super.setUp();
random = EasyMock.createMock(Random.class);
Persistence<byte[]> bytePersistence =
new MapBasedPersistence<byte[]>();
datastore = new LongPersistence(bytePersistence);
memcache = EasyMock.createMock(MemcacheService.class);
counter =
new Counter(random, bytePersistence, memcache,
CHANCE, "tst", NUM_SHARDS);
}

public void testEmptyStoreAndEmptyMemcacheWithSave() {

// Get a value from the empty memcache. Set the memcache
// to 0, since there is nothing in the store
EasyMock.expect(memcache.get(MEMCACHE_KEY)).andReturn(
null).once();
EasyMock.expect(
memcache.put(MEMCACHE_KEY, 0L, null,
SetPolicy.ADD_ONLY_IF_NOT_PRESENT)).andReturn(
true).once();

// Before incrementing, check that the memcache is
// populated
EasyMock.expect(memcache.get(MEMCACHE_KEY)).andReturn(
0L).once();
EasyMock.expect(memcache.increment(MEMCACHE_KEY, 2))
.andReturn(2L).once();

// Test if we need to write (0.0 means always yes). If
// so, pick "random shard" 3
EasyMock.expect(random.nextDouble()).andReturn(0.0)
.once();
EasyMock.expect(random.nextInt(NUM_SHARDS))
.andReturn(3).once();

// Now, there should be a value in memcache, so let's
// try to look it up.
// We return a different value than what we stored,
// which simulates a different
// process having increased the counter
EasyMock.expect(memcache.get(MEMCACHE_KEY)).andReturn(
5L).once();

// Now, let's replay the test and see if all
// expectations are met
EasyMock.replay(random, memcache);
assertEquals(0L, counter.get());
assertEquals(2L, counter.increment(2));
assertEquals(5L, counter.get());
assertEquals(2L, (long) datastore.get(STORE_PREFIX + 3));
EasyMock.verify(random, memcache);
}

public void testPrepopulatedStoreAndEmptyMemcache() {
datastore.mutate(STORE_PREFIX + 5, Functions
.constant(10L));
datastore.mutate(STORE_PREFIX + 7, Functions
.constant(1L));

// Get a value from the empty memcache. Set the memcache
// to 10, since that is the max in the store
EasyMock.expect(memcache.get(MEMCACHE_KEY)).andReturn(
null).once();
EasyMock.expect(
memcache.put(MEMCACHE_KEY, 10L, null,
SetPolicy.ADD_ONLY_IF_NOT_PRESENT)).andReturn(
true).once();

// Now, let's replay the test and see if all
// expectations are met
EasyMock.replay(random, memcache);
assertEquals(10L, counter.get());
EasyMock.verify(random, memcache);
}

public void testPrepopulatedStoreAndEmptyMemcacheUpdateFail() {
datastore.mutate(STORE_PREFIX + 5, Functions
.constant(10L));
datastore.mutate(STORE_PREFIX + 7, Functions
.constant(1L));

// Get a value from the empty memcache. Set the memcache
// to 10, since that is the max in the store.
// We let the update fail by returning false
EasyMock.expect(memcache.get(MEMCACHE_KEY)).andReturn(
null).once();
EasyMock.expect(
memcache.put(MEMCACHE_KEY, 10L, null,
SetPolicy.ADD_ONLY_IF_NOT_PRESENT)).andReturn(
false).once();

// Since the update failed, we expect the counter to do
// an additional memcache check to get the latest and
// greatest value
EasyMock.expect(memcache.get(MEMCACHE_KEY)).andReturn(
17L).once();

// Now, let's replay the test and see if all
// expectations are met
EasyMock.replay(random, memcache);
assertEquals(17L, counter.get());
EasyMock.verify(random, memcache);
}

public void testIncrementCounterWithSave() {

// Current counter is in memcache, so we can leave the
// store empty for now
EasyMock.expect(memcache.get(MEMCACHE_KEY)).andReturn(
10L).once();

// Perform an increment
EasyMock.expect(memcache.increment(MEMCACHE_KEY, 2))
.andReturn(4L).once();

// Test if we need to write (0.5 barely means yes). If
// so, pick "random shard" 8
// we need one additional memcache.get, because
// increment will check if the memcache is initialized
EasyMock.expect(memcache.get(MEMCACHE_KEY)).andReturn(
10L).once();
EasyMock.expect(random.nextDouble()).andReturn(0.5)
.once();
EasyMock.expect(random.nextInt(NUM_SHARDS))
.andReturn(8).once();

// Now replay and check the datastore
EasyMock.replay(random, memcache);
assertEquals(10L, counter.get());
assertEquals(4L, counter.increment(2));
assertEquals((Long) 4L, datastore.get(STORE_PREFIX + 8));
EasyMock.verify(random, memcache);
}

public void testIncrementCounterNoSave() {

// Current counter is in memcache, so we can leave the
// store empty for now
EasyMock.expect(memcache.get(MEMCACHE_KEY)).andReturn(
10L).once();

// Perform an increment
EasyMock.expect(memcache.increment(MEMCACHE_KEY, 2))
.andReturn(4L).once();

// Test if we need to write (0.6 means no).
// We need one additional memcache.get, because
// increment will check if the memcache is initialized.
// We expect no call do random.nextDouble(), which means
// that no shard is picked.
EasyMock.expect(memcache.get(MEMCACHE_KEY)).andReturn(
10L).once();
EasyMock.expect(random.nextDouble()).andReturn(0.6)
.once();

// Now replay and check the datastore
EasyMock.replay(random, memcache);
assertEquals(10L, counter.get());
assertEquals(4L, counter.increment(2));
EasyMock.verify(random, memcache);
}

/**
* Replicates one of the unit tests on the in-memory
* backend.
*/
public void testEmptyStoreAndEmptyMemcacheWithSaveForInMemoryAppEngine()
throws Exception {

// Set up in-memory App Engine
DatastorePersistence bytePersistence =
new DatastorePersistence("foo");
datastore = new LongPersistence(bytePersistence);
memcache = MemcacheServiceFactory.getMemcacheService();
counter =
new Counter(random, bytePersistence, memcache,
CHANCE, "tst", NUM_SHARDS);

// Run the test
EasyMock.expect(random.nextDouble()).andReturn(0.0);
EasyMock.expect(random.nextInt(NUM_SHARDS))
.andReturn(3);
EasyMock.replay(random);
assertEquals(0L, counter.get());
assertEquals(2L, counter.increment(2));
assertEquals(2L, counter.get());
assertEquals(2L, (long) datastore.get(STORE_PREFIX + 3));
assertEquals(2L, memcache.get(MEMCACHE_KEY));
EasyMock.verify(random);
}
}

Change log

r11 by schefflerjens on May 4, 2009   Diff
Experimental: tool classes for JDO unit
tests

Go to: 
Project members, sign in to write a code review

Older revisions

r7 by schefflerjens on Apr 22, 2009   Diff
The unit test passes -- counters
should now be ready to try out :-)
r6 by schefflerjens on Apr 22, 2009   Diff
Experimental implementation of an
efficient counter/id generator.
Not ready for real-life use yet (unit
test with in-memory App Engine
currently fails)
...
All revisions of this file

File info

Size: 8371 bytes, 250 lines
Powered by Google Project Hosting