blob: 252952325d59a4172580a3d2302d9a5f4a2ae050 [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 java.util.Collection;
Yuta HIGUCHI1edc36b2018-01-24 23:39:06 -080019import java.util.Collections;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070020
21import io.atomix.protocols.raft.ReadConsistency;
22import io.atomix.protocols.raft.cluster.MemberId;
Jordan Halterman035231e2017-07-18 08:39:07 -070023import io.atomix.protocols.raft.impl.RaftContext;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070024import 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;
Jordan Haltermane372d852017-11-02 15:00:06 -070038import io.atomix.utils.concurrent.AtomixThreadFactory;
39import io.atomix.utils.concurrent.SingleThreadContextFactory;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070040import io.atomix.utils.concurrent.ThreadContext;
41import org.junit.Test;
42import org.onosproject.store.service.Task;
43
44import static org.easymock.EasyMock.expect;
45import static org.easymock.EasyMock.mock;
46import static org.easymock.EasyMock.replay;
47import static org.junit.Assert.assertArrayEquals;
48import static org.junit.Assert.assertEquals;
49import static org.junit.Assert.assertNotNull;
50import static org.onosproject.store.primitives.resources.impl.AtomixWorkQueueOperations.ADD;
51import static org.onosproject.store.primitives.resources.impl.AtomixWorkQueueOperations.TAKE;
52import static org.onosproject.store.service.DistributedPrimitive.Type.WORK_QUEUE;
53
54/**
55 * Work queue service test.
56 */
57public class AtomixWorkQueueServiceTest {
58 @Test
59 public void testSnapshot() throws Exception {
60 SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder()
61 .withPrefix("test")
62 .withStorageLevel(StorageLevel.MEMORY)
63 .build());
Jordan Haltermane372d852017-11-02 15:00:06 -070064 Snapshot snapshot = store.newSnapshot(ServiceId.from(1), "test", 2, new WallClockTimestamp());
Jordan Halterman2bf177c2017-06-29 01:49:08 -070065
66 DefaultServiceContext context = mock(DefaultServiceContext.class);
67 expect(context.serviceType()).andReturn(ServiceType.from(WORK_QUEUE.name())).anyTimes();
68 expect(context.serviceName()).andReturn("test").anyTimes();
69 expect(context.serviceId()).andReturn(ServiceId.from(1)).anyTimes();
70 expect(context.executor()).andReturn(mock(ThreadContext.class)).anyTimes();
71
Jordan Halterman035231e2017-07-18 08:39:07 -070072 RaftContext server = mock(RaftContext.class);
Jordan Halterman2bf177c2017-06-29 01:49:08 -070073 expect(server.getProtocol()).andReturn(mock(RaftServerProtocol.class));
74
75 replay(context, server);
76
77 RaftSessionContext session = new RaftSessionContext(
78 SessionId.from(1),
79 MemberId.from("1"),
80 "test",
81 ServiceType.from(WORK_QUEUE.name()),
82 ReadConsistency.LINEARIZABLE,
Jordan Haltermane372d852017-11-02 15:00:06 -070083 100,
Jordan Halterman2bf177c2017-06-29 01:49:08 -070084 5000,
Jordan Halterman067e11b2017-12-08 20:39:09 -080085 System.currentTimeMillis(),
Jordan Halterman2bf177c2017-06-29 01:49:08 -070086 context,
Jordan Halterman035231e2017-07-18 08:39:07 -070087 server,
Jordan Haltermane372d852017-11-02 15:00:06 -070088 new SingleThreadContextFactory(new AtomixThreadFactory()));
Jordan Halterman2bf177c2017-06-29 01:49:08 -070089
90 AtomixWorkQueueService service = new AtomixWorkQueueService();
91 service.init(context);
92
93 service.add(new DefaultCommit<>(
94 2,
95 ADD,
Yuta HIGUCHI1edc36b2018-01-24 23:39:06 -080096 new AtomixWorkQueueOperations.Add(Collections.singletonList("Hello world!".getBytes())),
Jordan Halterman2bf177c2017-06-29 01:49:08 -070097 session,
98 System.currentTimeMillis()));
99
100 try (SnapshotWriter writer = snapshot.openWriter()) {
101 service.snapshot(writer);
102 }
103
104 snapshot.complete();
105
106 service = new AtomixWorkQueueService();
107 service.init(context);
108
109 try (SnapshotReader reader = snapshot.openReader()) {
110 service.install(reader);
111 }
112
113 Collection<Task<byte[]>> value = service.take(new DefaultCommit<>(
114 2,
115 TAKE,
116 new AtomixWorkQueueOperations.Take(1),
117 session,
118 System.currentTimeMillis()));
119 assertNotNull(value);
120 assertEquals(1, value.size());
121 assertArrayEquals("Hello world!".getBytes(), value.iterator().next().payload());
122 }
123}