Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.store.primitives.resources.impl; |
| 17 | |
| 18 | import io.atomix.protocols.raft.service.ServiceId; |
| 19 | import io.atomix.protocols.raft.service.impl.DefaultCommit; |
| 20 | import io.atomix.protocols.raft.session.impl.RaftSessionContext; |
| 21 | import io.atomix.protocols.raft.storage.RaftStorage; |
| 22 | import io.atomix.protocols.raft.storage.snapshot.Snapshot; |
| 23 | import io.atomix.protocols.raft.storage.snapshot.SnapshotReader; |
| 24 | import io.atomix.protocols.raft.storage.snapshot.SnapshotStore; |
| 25 | import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter; |
| 26 | import io.atomix.storage.StorageLevel; |
| 27 | import io.atomix.time.WallClockTimestamp; |
| 28 | import org.junit.Test; |
| 29 | |
| 30 | import static org.easymock.EasyMock.mock; |
| 31 | import static org.junit.Assert.assertEquals; |
| 32 | import static org.onosproject.store.primitives.resources.impl.AtomixAtomicCounterMapOperations.GET; |
| 33 | import static org.onosproject.store.primitives.resources.impl.AtomixAtomicCounterMapOperations.PUT; |
| 34 | |
| 35 | /** |
| 36 | * Atomic counter map service test. |
| 37 | */ |
| 38 | public 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 | } |