blob: 6d7007a67f240bf39d55ab2f244be726ec3ad802 [file] [log] [blame]
Jordan Halterman2bf177c2017-06-29 01:49:08 -07001/*
2 * Copyright 2017-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.store.primitives.resources.impl;
17
18import io.atomix.protocols.raft.service.ServiceId;
19import io.atomix.protocols.raft.service.impl.DefaultCommit;
20import io.atomix.protocols.raft.session.impl.RaftSessionContext;
21import io.atomix.protocols.raft.storage.RaftStorage;
22import io.atomix.protocols.raft.storage.snapshot.Snapshot;
23import io.atomix.protocols.raft.storage.snapshot.SnapshotReader;
24import io.atomix.protocols.raft.storage.snapshot.SnapshotStore;
25import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter;
26import io.atomix.storage.StorageLevel;
27import io.atomix.time.WallClockTimestamp;
28import org.junit.Test;
29
30import static org.easymock.EasyMock.mock;
31import static org.junit.Assert.assertEquals;
32import static org.onosproject.store.primitives.resources.impl.AtomixAtomicCounterMapOperations.GET;
33import static org.onosproject.store.primitives.resources.impl.AtomixAtomicCounterMapOperations.PUT;
34
35/**
36 * Atomic counter map service test.
37 */
38public class AtomixAtomicCounterMapServiceTest {
39 @Test
40 public void testSnapshot() throws Exception {
41 SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder()
42 .withPrefix("test")
43 .withStorageLevel(StorageLevel.MEMORY)
44 .build());
45 Snapshot snapshot = store.newSnapshot(ServiceId.from(1), 2, new WallClockTimestamp());
46
47 AtomixAtomicCounterMapService service = new AtomixAtomicCounterMapService();
48 service.put(new DefaultCommit<>(
49 2,
50 PUT,
51 new AtomixAtomicCounterMapOperations.Put("foo", 1),
52 mock(RaftSessionContext.class),
53 System.currentTimeMillis()));
54
55 try (SnapshotWriter writer = snapshot.openWriter()) {
56 service.snapshot(writer);
57 }
58
59 snapshot.complete();
60
61 service = new AtomixAtomicCounterMapService();
62 try (SnapshotReader reader = snapshot.openReader()) {
63 service.install(reader);
64 }
65
66 long value = service.get(new DefaultCommit<>(
67 2,
68 GET,
69 new AtomixAtomicCounterMapOperations.Get("foo"),
70 mock(RaftSessionContext.class),
71 System.currentTimeMillis()));
72 assertEquals(1, value);
73 }
74}