Madan Jampani
Committed by Gerrit Code Review

stc scenario for testing work queue distributed primitive

Change-Id: Ib548cef733a7d1f6418d3c318aa41d5e2cd1f400
1 +/*
2 + * Copyright 2016-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 + */
16 +package org.onosproject.distributedprimitives.cli;
17 +
18 +import java.util.Arrays;
19 +import java.util.Collection;
20 +import java.util.concurrent.CompletableFuture;
21 +import java.util.concurrent.TimeUnit;
22 +
23 +import org.apache.karaf.shell.commands.Argument;
24 +import org.apache.karaf.shell.commands.Command;
25 +import org.onosproject.cli.AbstractShellCommand;
26 +import org.onosproject.store.serializers.KryoNamespaces;
27 +import org.onosproject.store.service.Serializer;
28 +import org.onosproject.store.service.StorageService;
29 +import org.onosproject.store.service.Task;
30 +import org.onosproject.store.service.WorkQueue;
31 +import org.onosproject.store.service.WorkQueueStats;
32 +
33 +import com.google.common.base.Throwables;
34 +
35 +/**
36 + * CLI command to test a distributed work queue.
37 + */
38 +@Command(scope = "onos", name = "work-queue-test",
39 + description = "Test a distributed work queue")
40 +public class WorkQueueTestCommand extends AbstractShellCommand {
41 +
42 + @Argument(index = 0, name = "name",
43 + description = "Work Queue name",
44 + required = true, multiValued = false)
45 + String name = null;
46 +
47 + @Argument(index = 1, name = "operation",
48 + description = "operation name. One of {add, addMutiple, "
49 + + "takeAndComplete, totalPending, totalInProgress, totalCompleted}",
50 + required = true, multiValued = false)
51 + String operation = null;
52 +
53 + @Argument(index = 2, name = "value1",
54 + description = "first arg",
55 + required = false, multiValued = false)
56 + String value1 = null;
57 +
58 + @Argument(index = 3, name = "value2",
59 + description = "second arg",
60 + required = false, multiValued = false)
61 + String value2 = null;
62 +
63 + WorkQueue<String> queue;
64 +
65 + @Override
66 + protected void execute() {
67 + StorageService storageService = get(StorageService.class);
68 + Serializer serializer = Serializer.using(KryoNamespaces.BASIC);
69 + queue = storageService.getWorkQueue(name, serializer);
70 + if (operation.equals("add")) {
71 + if (value1 == null) {
72 + print("Usage: add <value1>");
73 + } else {
74 + get(queue.addOne(value1));
75 + print("Done");
76 + }
77 + } else if (operation.equals("addMultiple")) {
78 + if (value1 == null || value2 == null) {
79 + print("Usage: addMultiple <value1> <value2>");
80 + } else {
81 + get(queue.addMultiple(Arrays.asList(value1, value2)));
82 + print("Done");
83 + }
84 + } else if (operation.equals("takeAndComplete")) {
85 + int maxItems = value1 != null ? Integer.parseInt(value1) : 1;
86 + Collection<Task<String>> tasks = get(queue.take(maxItems));
87 + tasks.forEach(task -> get(queue.complete(task.taskId())));
88 + print("Done");
89 + } else if (operation.equals("totalPending")) {
90 + WorkQueueStats stats = get(queue.stats());
91 + print("%d", stats.totalPending());
92 + } else if (operation.equals("totalInProgress")) {
93 + WorkQueueStats stats = get(queue.stats());
94 + print("%d", stats.totalInProgress());
95 + } else if (operation.equals("totalCompleted")) {
96 + WorkQueueStats stats = get(queue.stats());
97 + print("%d", stats.totalCompleted());
98 + } else {
99 + print("Invalid operation name. Valid operations names are:"
100 + + " [add, addMultiple takeAndComplete, totalPending, totalInProgress, totalCompleted]");
101 + }
102 + }
103 +
104 + private <T> T get(CompletableFuture<T> future) {
105 + try {
106 + return future.get(1, TimeUnit.SECONDS);
107 + } catch (Exception e) {
108 + throw Throwables.propagate(e);
109 + }
110 + }
111 +}
...@@ -38,6 +38,9 @@ ...@@ -38,6 +38,9 @@
38 <action class="org.onosproject.distributedprimitives.cli.CounterTestCommand"/> 38 <action class="org.onosproject.distributedprimitives.cli.CounterTestCommand"/>
39 </command> 39 </command>
40 <command> 40 <command>
41 + <action class="org.onosproject.distributedprimitives.cli.WorkQueueTestCommand"/>
42 + </command>
43 + <command>
41 <action class="org.onosproject.distributedprimitives.cli.ConsistentMapTestCommand"/> 44 <action class="org.onosproject.distributedprimitives.cli.ConsistentMapTestCommand"/>
42 </command> 45 </command>
43 <command> 46 <command>
......
1 +<!--
2 + ~ Copyright 2016-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 +-->
16 +
17 +<scenario name="distributed-work-queue-test"
18 + description="ONOS WorkQueue distributed primitive Test">
19 + <group name="Distributed-Primitive-WorkQueue">
20 +
21 + <step name="Distributed-Primitive-WorkQueue.Activate-Distributed-Primitives-App"
22 + exec="onos ${OCI} app activate org.onosproject.distributedprimitives"/>
23 +
24 + <step name="Distributed-Primitive-WorkQueue.Test-Queue-AddOne" requires="^"
25 + exec="onos-execute-expect ${OCI} work-queue-test stc-test-work-queue add foo --expect Done"/>
26 +
27 + <step name="Distributed-Primitive-WorkQueue.Test-Queue-Check-Pending-1" requires="^"
28 + exec="onos-cluster-execute-expect work-queue-test stc-test-work-queue totalPending --expect 1"/>
29 +
30 + <step name="Distributed-Primitive-WorkQueue.Test-Queue-Check-InProgress-1" requires="^"
31 + exec="onos-cluster-execute-expect work-queue-test stc-test-work-queue totalInProgress --expect 0"/>
32 +
33 + <step name="Distributed-Primitive-WorkQueue.Test-Queue-AddMultiple" requires="^"
34 + exec="onos-execute-expect ${OCI} work-queue-test stc-test-work-queue addMultiple bar car --expect Done"/>
35 +
36 + <step name="Distributed-Primitive-WorkQueue.Test-Queue-TakeAndComplete" requires="^"
37 + exec="onos-execute-expect ${OCI} work-queue-test stc-test-work-queue takeAndComplete 3 --expect Done"/>
38 +
39 + <step name="Distributed-Primitive-WorkQueue.Test-Queue-Check-Pending-2" requires="^"
40 + exec="onos-cluster-execute-expect work-queue-test stc-test-work-queue totalPending --expect 0"/>
41 +
42 + <step name="Distributed-Primitive-WorkQueue.Test-Queue-Check-InProgress-2" requires="^"
43 + exec="onos-cluster-execute-expect work-queue-test stc-test-work-queue totalInProgress --expect 0"/>
44 +
45 + <!-- Since totalCompleted is a additive quantity, testing its value breaks when the test is run in a loop -->
46 +
47 + <!--Check with check logs-->
48 + <step name="Distributed-Primitive-WorkQueue.Check-Log-Exceptions" requires="^"
49 + exec="onos-check-logs ${OCI}"/>
50 +
51 + <step name="Distributed-Primitive-WorkQueue.Teardown-Distributed-Primitives-Test-App" requires="^"
52 + exec="onos ${OCI} app deactivate org.onosproject.distributedprimitives"/>
53 + </group>
54 +</scenario>
55 +