blob: 44ebd52ef104aa64a10d2aeccb53956862bab44b [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 java.util.Arrays;
19import java.util.Collection;
20
21import io.atomix.protocols.raft.ReadConsistency;
22import io.atomix.protocols.raft.cluster.MemberId;
23import io.atomix.protocols.raft.impl.RaftServerContext;
24import io.atomix.protocols.raft.protocol.RaftServerProtocol;
25import io.atomix.protocols.raft.service.ServiceId;
26import io.atomix.protocols.raft.service.ServiceType;
27import io.atomix.protocols.raft.service.impl.DefaultCommit;
28import io.atomix.protocols.raft.service.impl.DefaultServiceContext;
29import io.atomix.protocols.raft.session.SessionId;
30import io.atomix.protocols.raft.session.impl.RaftSessionContext;
31import io.atomix.protocols.raft.storage.RaftStorage;
32import io.atomix.protocols.raft.storage.snapshot.Snapshot;
33import io.atomix.protocols.raft.storage.snapshot.SnapshotReader;
34import io.atomix.protocols.raft.storage.snapshot.SnapshotStore;
35import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter;
36import io.atomix.storage.StorageLevel;
37import io.atomix.time.WallClockTimestamp;
38import io.atomix.utils.concurrent.ThreadContext;
39import org.junit.Test;
40import org.onosproject.store.service.Task;
41
42import static org.easymock.EasyMock.expect;
43import static org.easymock.EasyMock.mock;
44import static org.easymock.EasyMock.replay;
45import static org.junit.Assert.assertArrayEquals;
46import static org.junit.Assert.assertEquals;
47import static org.junit.Assert.assertNotNull;
48import static org.onosproject.store.primitives.resources.impl.AtomixWorkQueueOperations.ADD;
49import static org.onosproject.store.primitives.resources.impl.AtomixWorkQueueOperations.TAKE;
50import static org.onosproject.store.service.DistributedPrimitive.Type.WORK_QUEUE;
51
52/**
53 * Work queue service test.
54 */
55public class AtomixWorkQueueServiceTest {
56 @Test
57 public void testSnapshot() throws Exception {
58 SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder()
59 .withPrefix("test")
60 .withStorageLevel(StorageLevel.MEMORY)
61 .build());
62 Snapshot snapshot = store.newSnapshot(ServiceId.from(1), 2, new WallClockTimestamp());
63
64 DefaultServiceContext context = mock(DefaultServiceContext.class);
65 expect(context.serviceType()).andReturn(ServiceType.from(WORK_QUEUE.name())).anyTimes();
66 expect(context.serviceName()).andReturn("test").anyTimes();
67 expect(context.serviceId()).andReturn(ServiceId.from(1)).anyTimes();
68 expect(context.executor()).andReturn(mock(ThreadContext.class)).anyTimes();
69
70 RaftServerContext server = mock(RaftServerContext.class);
71 expect(server.getProtocol()).andReturn(mock(RaftServerProtocol.class));
72
73 replay(context, server);
74
75 RaftSessionContext session = new RaftSessionContext(
76 SessionId.from(1),
77 MemberId.from("1"),
78 "test",
79 ServiceType.from(WORK_QUEUE.name()),
80 ReadConsistency.LINEARIZABLE,
81 5000,
82 context,
83 server);
84
85 AtomixWorkQueueService service = new AtomixWorkQueueService();
86 service.init(context);
87
88 service.add(new DefaultCommit<>(
89 2,
90 ADD,
91 new AtomixWorkQueueOperations.Add(Arrays.asList("Hello world!".getBytes())),
92 session,
93 System.currentTimeMillis()));
94
95 try (SnapshotWriter writer = snapshot.openWriter()) {
96 service.snapshot(writer);
97 }
98
99 snapshot.complete();
100
101 service = new AtomixWorkQueueService();
102 service.init(context);
103
104 try (SnapshotReader reader = snapshot.openReader()) {
105 service.install(reader);
106 }
107
108 Collection<Task<byte[]>> value = service.take(new DefaultCommit<>(
109 2,
110 TAKE,
111 new AtomixWorkQueueOperations.Take(1),
112 session,
113 System.currentTimeMillis()));
114 assertNotNull(value);
115 assertEquals(1, value.size());
116 assertArrayEquals("Hello world!".getBytes(), value.iterator().next().payload());
117 }
118}