blob: 9d58758fa377d91020264d82aa0d64fa7843aa5a [file] [log] [blame]
Jordan Halterman2bf177c2017-06-29 01:49:08 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jordan Halterman2bf177c2017-06-29 01:49:08 -07003 *
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
Jordan Halterman2bf177c2017-06-29 01:49:08 -070018import io.atomix.protocols.raft.service.impl.DefaultCommit;
19import io.atomix.protocols.raft.session.impl.RaftSessionContext;
20import io.atomix.protocols.raft.storage.RaftStorage;
21import io.atomix.protocols.raft.storage.snapshot.Snapshot;
22import io.atomix.protocols.raft.storage.snapshot.SnapshotReader;
23import io.atomix.protocols.raft.storage.snapshot.SnapshotStore;
24import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter;
25import io.atomix.storage.StorageLevel;
26import io.atomix.time.WallClockTimestamp;
27import org.junit.Test;
28
29import static org.easymock.EasyMock.mock;
30import static org.junit.Assert.assertEquals;
31import static org.onosproject.store.primitives.resources.impl.AtomixAtomicCounterMapOperations.GET;
32import static org.onosproject.store.primitives.resources.impl.AtomixAtomicCounterMapOperations.PUT;
33
34/**
35 * Atomic counter map service test.
36 */
37public class AtomixAtomicCounterMapServiceTest {
38 @Test
39 public void testSnapshot() throws Exception {
40 SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder()
41 .withPrefix("test")
42 .withStorageLevel(StorageLevel.MEMORY)
43 .build());
Jordan Halterman4ce65e82018-02-15 18:09:38 -080044 Snapshot snapshot = store.newSnapshot(2, new WallClockTimestamp());
Jordan Halterman2bf177c2017-06-29 01:49:08 -070045
46 AtomixAtomicCounterMapService service = new AtomixAtomicCounterMapService();
47 service.put(new DefaultCommit<>(
48 2,
49 PUT,
50 new AtomixAtomicCounterMapOperations.Put("foo", 1),
51 mock(RaftSessionContext.class),
52 System.currentTimeMillis()));
53
54 try (SnapshotWriter writer = snapshot.openWriter()) {
55 service.snapshot(writer);
56 }
57
58 snapshot.complete();
59
60 service = new AtomixAtomicCounterMapService();
61 try (SnapshotReader reader = snapshot.openReader()) {
62 service.install(reader);
63 }
64
65 long value = service.get(new DefaultCommit<>(
66 2,
67 GET,
68 new AtomixAtomicCounterMapOperations.Get("foo"),
69 mock(RaftSessionContext.class),
70 System.currentTimeMillis()));
71 assertEquals(1, value);
72 }
73}