aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/processing/ResponseTestCase.java
blob: 595cbb14affe99c8d8806579794d4428cd61b44d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.processing;

import com.yahoo.processing.response.ArrayDataList;
import com.yahoo.processing.response.DataList;
import com.yahoo.processing.test.ProcessorLibrary;
import com.yahoo.processing.test.Responses;
import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutionException;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * @author  bratseth
 */
@SuppressWarnings("unchecked")
public class ResponseTestCase {

    /**
     * Create a nested async tree of data elements, complete it recursively and check completion order.
     * Check the recursive toString printing along the way.
     * List variable names ends by numbers specifying the index of the list at each level.
     */
    @SuppressWarnings({"unchecked"})
    @Test
    void testRecursiveCompletionAndToString() throws InterruptedException, ExecutionException {
        // create lists
        Request request = new Request();
        DataList list1 = ArrayDataList.create(request);
        DataList list11 = ArrayDataList.create(request);
        DataList list12 = ArrayDataList.createAsync(request);
        DataList list13 = ArrayDataList.createAsync(request);
        DataList list14 = ArrayDataList.create(request);
        DataList list121 = ArrayDataList.createAsync(request);
        DataList list122 = ArrayDataList.create(request);
        DataList list123 = ArrayDataList.createAsync(request);
        DataList list1231 = ArrayDataList.createAsync(request);
        DataList list1232 = ArrayDataList.create(request);
        // wire tree
        list1.add(list11);
        list1.add(list12);
        list1.add(list13);
        list1.add(list14);
        list12.add(list121);
        list12.add(list122);
        list12.add(list123);
        list123.add(list1231);
        list123.add(list1232);
        // add sync data elements
        list1.add(new ProcessorLibrary.StringData(request, "list1"));
        list12.add(new ProcessorLibrary.StringData(request, "list12"));
        list14.add(new ProcessorLibrary.StringData(request, "list14"));
        list122.add(new ProcessorLibrary.StringData(request, "list122"));
        list1231.add(new ProcessorLibrary.StringData(request, "list1231"));

        assertEqualsIgnoreObjectNumbers("Uncompleted tree, no incoming", uncompletedTreeUncompletedIncoming, Responses.recursiveToString(list1));

        // provide all async incoming data
        list12.incoming().markComplete();
        list121.incoming().addLast(new ProcessorLibrary.StringData(request, "list121async1"));
        list123.incoming().markComplete();
        list1231.incoming().add(new ProcessorLibrary.StringData(request, "list13231async1"));
        list1231.incoming().addLast(new ProcessorLibrary.StringData(request, "list1231async2"));
        list13.incoming().add(new ProcessorLibrary.StringData(request, "list13async1"));
        list13.incoming().addLast(new ProcessorLibrary.StringData(request, "list13async2"));

        assertEqualsIgnoreObjectNumbers("Uncompleted tree, incoming complete", uncompletedTreeCompletedIncoming, Responses.recursiveToString(list1));

        // complete all
        Response.recursiveFuture(list1).get();
        assertEqualsIgnoreObjectNumbers("Completed tree", completedTree, Responses.recursiveToString(list1));
    }

    private void assertEqualsIgnoreObjectNumbers(String explanation,String expected,String actual) {
        assertEquals(expected,removeObjectNumbers(actual),explanation);
    }

    /** Removes all object numbers (occurrences of @hexnumber) */
    private String removeObjectNumbers(String s) {
        return s.replaceAll("@[0-9a-f]+","");
    }

    private static final String uncompletedTreeUncompletedIncoming=
            "com.yahoo.processing.response.ArrayDataList [incomplete, (no incoming)]\n" +
                    "    com.yahoo.processing.response.ArrayDataList [incomplete, (no incoming)]\n" +
                    "    com.yahoo.processing.response.ArrayDataList [incomplete, incoming: incomplete, data []]\n" +
                    "        com.yahoo.processing.response.ArrayDataList [incomplete, incoming: incomplete, data []]\n" +
                    "        com.yahoo.processing.response.ArrayDataList [incomplete, (no incoming)]\n" +
                    "            list122\n" +
                    "        com.yahoo.processing.response.ArrayDataList [incomplete, incoming: incomplete, data []]\n" +
                    "            com.yahoo.processing.response.ArrayDataList [incomplete, incoming: incomplete, data []]\n" +
                    "                list1231\n" +
                    "            com.yahoo.processing.response.ArrayDataList [incomplete, (no incoming)]\n" +
                    "        list12\n" +
                    "    com.yahoo.processing.response.ArrayDataList [incomplete, incoming: incomplete, data []]\n" +
                    "    com.yahoo.processing.response.ArrayDataList [incomplete, (no incoming)]\n" +
                    "        list14\n" +
                    "    list1\n";

    private static final String uncompletedTreeCompletedIncoming=
                    "com.yahoo.processing.response.ArrayDataList [incomplete, (no incoming)]\n" +
                    "    com.yahoo.processing.response.ArrayDataList [incomplete, (no incoming)]\n" +
                    "    com.yahoo.processing.response.ArrayDataList [incomplete, incoming: complete, data []]\n" +
                    "        com.yahoo.processing.response.ArrayDataList [incomplete, incoming: complete, data [list121async1]]\n" +
                    "        com.yahoo.processing.response.ArrayDataList [incomplete, (no incoming)]\n" +
                    "            list122\n" +
                    "        com.yahoo.processing.response.ArrayDataList [incomplete, incoming: complete, data []]\n" +
                    "            com.yahoo.processing.response.ArrayDataList [incomplete, incoming: complete, data [list13231async1, list1231async2]]\n" +
                    "                list1231\n" +
                    "            com.yahoo.processing.response.ArrayDataList [incomplete, (no incoming)]\n" +
                    "        list12\n" +
                    "    com.yahoo.processing.response.ArrayDataList [incomplete, incoming: complete, data [list13async1, list13async2]]\n" +
                    "    com.yahoo.processing.response.ArrayDataList [incomplete, (no incoming)]\n" +
                    "        list14\n" +
                    "    list1\n";

    private static final String completedTree=
                    "com.yahoo.processing.response.ArrayDataList [completed]\n" +
                    "    com.yahoo.processing.response.ArrayDataList [completed]\n" +
                    "    com.yahoo.processing.response.ArrayDataList [completed]\n" +
                    "        com.yahoo.processing.response.ArrayDataList [completed]\n" +
                    "            list121async1\n" +
                    "        com.yahoo.processing.response.ArrayDataList [completed]\n" +
                    "            list122\n" +
                    "        com.yahoo.processing.response.ArrayDataList [completed]\n" +
                    "            com.yahoo.processing.response.ArrayDataList [completed]\n" +
                    "                list1231\n" +
                    "                list13231async1\n" +
                    "                list1231async2\n" +
                    "            com.yahoo.processing.response.ArrayDataList [completed]\n" +
                    "        list12\n" +
                    "    com.yahoo.processing.response.ArrayDataList [completed]\n" +
                    "        list13async1\n" +
                    "        list13async2\n" +
                    "    com.yahoo.processing.response.ArrayDataList [completed]\n" +
                    "        list14\n" +
                    "    list1\n";
}