blob: fdc1d9776e4d86b6a562b2c398c84a91d303cdd9 [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 HIGUCHIfbd9ae92018-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 Halterman19486e32017-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 org.junit.Test;
41import org.onosproject.store.service.Task;
42
43import static org.easymock.EasyMock.expect;
44import static org.easymock.EasyMock.mock;
45import static org.easymock.EasyMock.replay;
46import static org.junit.Assert.assertArrayEquals;
47import static org.junit.Assert.assertEquals;
48import static org.junit.Assert.assertNotNull;
49import static org.onosproject.store.primitives.resources.impl.AtomixWorkQueueOperations.ADD;
50import static org.onosproject.store.primitives.resources.impl.AtomixWorkQueueOperations.TAKE;
51import static org.onosproject.store.service.DistributedPrimitive.Type.WORK_QUEUE;
52
53/**
54 * Work queue service test.
55 */
56public class AtomixWorkQueueServiceTest {
57 @Test
58 public void testSnapshot() throws Exception {
59 SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder()
60 .withPrefix("test")
61 .withStorageLevel(StorageLevel.MEMORY)
62 .build());
Jordan Halterman4ce65e82018-02-15 18:09:38 -080063 Snapshot snapshot = store.newSnapshot(2, new WallClockTimestamp());
Jordan Halterman2bf177c2017-06-29 01:49:08 -070064
65 DefaultServiceContext context = mock(DefaultServiceContext.class);
66 expect(context.serviceType()).andReturn(ServiceType.from(WORK_QUEUE.name())).anyTimes();
67 expect(context.serviceName()).andReturn("test").anyTimes();
68 expect(context.serviceId()).andReturn(ServiceId.from(1)).anyTimes();
Jordan Halterman2bf177c2017-06-29 01:49:08 -070069
Jordan Halterman035231e2017-07-18 08:39:07 -070070 RaftContext server = mock(RaftContext.class);
Jordan Halterman2bf177c2017-06-29 01:49:08 -070071 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,
Jordan Halterman19486e32017-11-02 15:00:06 -070081 100,
Jordan Halterman2bf177c2017-06-29 01:49:08 -070082 5000,
Jordan Haltermane9467fc2017-12-08 20:39:09 -080083 System.currentTimeMillis(),
Jordan Halterman2bf177c2017-06-29 01:49:08 -070084 context,
Jordan Halterman035231e2017-07-18 08:39:07 -070085 server,
Jordan Halterman19486e32017-11-02 15:00:06 -070086 new SingleThreadContextFactory(new AtomixThreadFactory()));
Jordan Halterman2bf177c2017-06-29 01:49:08 -070087
88 AtomixWorkQueueService service = new AtomixWorkQueueService();
89 service.init(context);
90
91 service.add(new DefaultCommit<>(
92 2,
93 ADD,
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -080094 new AtomixWorkQueueOperations.Add(Collections.singletonList("Hello world!".getBytes())),
Jordan Halterman2bf177c2017-06-29 01:49:08 -070095 session,
96 System.currentTimeMillis()));
97
98 try (SnapshotWriter writer = snapshot.openWriter()) {
99 service.snapshot(writer);
100 }
101
102 snapshot.complete();
103
104 service = new AtomixWorkQueueService();
105 service.init(context);
106
107 try (SnapshotReader reader = snapshot.openReader()) {
108 service.install(reader);
109 }
110
111 Collection<Task<byte[]>> value = service.take(new DefaultCommit<>(
112 2,
113 TAKE,
114 new AtomixWorkQueueOperations.Take(1),
115 session,
116 System.currentTimeMillis()));
117 assertNotNull(value);
118 assertEquals(1, value.size());
119 assertArrayEquals("Hello world!".getBytes(), value.iterator().next().payload());
120 }
121}