aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/test/java/com/yahoo/documentapi/messagebus/protocol/ReplyMergerTestCase.java
blob: 0fb8ae5c2be1c1445cad66fab7dbddf13db14a46 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.documentapi.messagebus.protocol;

import com.yahoo.collections.Tuple2;
import com.yahoo.messagebus.EmptyReply;
import com.yahoo.messagebus.Error;
import com.yahoo.messagebus.Reply;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

public class ReplyMergerTestCase {

    private ReplyMerger merger;

    @Before
    public void setUp() {
        merger = new ReplyMerger();
    }

    @Test
    public void mergingGenericRepliesWithNoErrorsPicksFirstReply() {
        Reply r1 = new EmptyReply();
        Reply r2 = new EmptyReply();
        Reply r3 = new EmptyReply();
        merger.merge(0, r1);
        merger.merge(1, r2);
        merger.merge(2, r3);
        Tuple2<Integer, Reply> ret = merger.mergedReply();

        assertEquals(0, ret.first.intValue());
        assertSame(r1, ret.second);
    }

    @Test
    public void mergingSingleReplyWithOneErrorReturnsSameReplyWithError() {
        Reply r1 = new EmptyReply();
        Error error = new Error(1234, "oh no!");
        r1.addError(error);
        merger.merge(0, r1);
        Tuple2<Integer, Reply> ret = merger.mergedReply();

        assertNull(ret.first);
        assertSame(r1, ret.second);
        assertThatErrorsMatch(new Error[] { error }, ret);
    }

    @Test
    public void mergingSingleReplyWithMultipleErrorsReturnsSameReplyWithAllErrors() {
        Reply r1 = new EmptyReply();
        Error errors[] = new Error[] {
                new Error(1234, "oh no!"), new Error(4567, "oh dear!"),
        };
        r1.addError(errors[0]);
        r1.addError(errors[1]);
        merger.merge(0, r1);
        Tuple2<Integer, Reply> ret = merger.mergedReply();

        assertNull(ret.first);
        assertSame(r1, ret.second);
        assertThatErrorsMatch(errors, ret);
    }

    @Test
    public void mergingMultipleRepliesWithMultipleErrorsReturnsMostSevereReplyWithAllErrors() {
        Reply r1 = new EmptyReply();
        Reply r2 = new EmptyReply();
        Error errors[] = new Error[] {
                new Error(1234, "oh no!"), new Error(4567, "oh dear!"), new Error(678, "omg!"),
        };
        r1.addError(errors[0]);
        r1.addError(errors[1]);
        r2.addError(errors[2]);
        merger.merge(0, r1);
        merger.merge(1, r2);
        Tuple2<Integer, Reply> ret = merger.mergedReply();

        assertNull(ret.first);
        assertSame(r1, ret.second);
        assertNotSame(r2, ret.second);
        assertThatErrorsMatch(errors, ret);
    }

    @Test
    public void returnIgnoredReplyWhenAllRepliesHaveOnlyIgnoredErrors() {
        Reply r1 = new EmptyReply();
        Reply r2 = new EmptyReply();
        Error errors[] = new Error[] {
                new Error(DocumentProtocol.ERROR_MESSAGE_IGNORED, "oh no!"),
                new Error(DocumentProtocol.ERROR_MESSAGE_IGNORED, "oh dear!"),
                new Error(DocumentProtocol.ERROR_MESSAGE_IGNORED, "omg!"),
        };
        r1.addError(errors[0]);
        r1.addError(errors[1]);
        r2.addError(errors[2]);

        merger.merge(0, r1);
        merger.merge(1, r2);
        Tuple2<Integer, Reply> ret = merger.mergedReply();
        assertNull(ret.first);
        assertNotSame(r1, ret.second);
        assertNotSame(r2, ret.second);
        // Only first ignore error from each reply
        assertThatErrorsMatch(new Error[]{ errors[0], errors[2] }, ret);
    }

    @Test
    public void successfulReplyTakesPrecedenceOverIgnoredReplyWhenNoErrors() {
        Reply r1 = new EmptyReply();
        Reply r2 = new EmptyReply();
        Error errors[] = new Error[] {
                new Error(DocumentProtocol.ERROR_MESSAGE_IGNORED, "oh no!"),
        };
        r1.addError(errors[0]);
        merger.merge(0, r1);
        merger.merge(1, r2);
        Tuple2<Integer, Reply> ret = merger.mergedReply();
        assertEquals(1, ret.first.intValue());
        assertSame(r2, ret.second);
        // Only first ignore error from each reply
        assertThatErrorsMatch(new Error[]{ }, ret);
    }

    @Test
    public void nonIgnoredErrorTakesPrecedence() {
        Reply r1 = new EmptyReply();
        Reply r2 = new EmptyReply();
        Error errors[] = new Error[] {
                new Error(DocumentProtocol.ERROR_MESSAGE_IGNORED, "oh no!"),
                new Error(DocumentProtocol.ERROR_ABORTED, "kablammo!"),
                new Error(DocumentProtocol.ERROR_MESSAGE_IGNORED, "omg!"),
        };
        r1.addError(errors[0]);
        r1.addError(errors[1]);
        r2.addError(errors[2]);

        merger.merge(0, r1);
        merger.merge(1, r2);
        Tuple2<Integer, Reply> ret = merger.mergedReply();
        assertNull(ret.first);
        assertSame(r1, ret.second);
        assertNotSame(r2, ret.second);
        // All errors from replies with errors are included, not those that
        // are fully ignored.
        assertThatErrorsMatch(new Error[]{ errors[0], errors[1] }, ret);
    }

    @Test
    public void returnRemoveDocumentReplyWhereDocWasFound() {
        RemoveDocumentReply r1 = new RemoveDocumentReply();
        RemoveDocumentReply r2 = new RemoveDocumentReply();
        RemoveDocumentReply r3 = new RemoveDocumentReply();
        r1.setWasFound(false);
        r2.setWasFound(true);
        r3.setWasFound(false);

        merger.merge(0, r1);
        merger.merge(1, r2);
        merger.merge(2, r3);
        Tuple2<Integer, Reply> ret = merger.mergedReply();
        assertEquals(1, ret.first.intValue());
        assertSame(r2, ret.second);
    }

    @Test
    public void returnFirstRemoveDocumentReplyIfNoDocsWereFound() {
        RemoveDocumentReply r1 = new RemoveDocumentReply();
        RemoveDocumentReply r2 = new RemoveDocumentReply();
        r1.setWasFound(false);
        r2.setWasFound(false);

        merger.merge(0, r1);
        merger.merge(1, r2);
        Tuple2<Integer, Reply> ret = merger.mergedReply();
        assertEquals(0, ret.first.intValue());
        assertSame(r1, ret.second);
    }

    @Test
    // TODO jonmv: This seems wrong, and is probably a consequence of TAS being implemented after reply merging.
    public void returnErrorDocumentReplyWhereDocWasFoundWhichIsProbablyWrong() {
        Error e1 = new Error(DocumentProtocol.ERROR_TEST_AND_SET_CONDITION_FAILED, "fail");
        UpdateDocumentReply r1 = new UpdateDocumentReply();
        UpdateDocumentReply r2 = new UpdateDocumentReply();
        UpdateDocumentReply r3 = new UpdateDocumentReply();
        r1.addError(e1); // return error
        r2.setWasFound(true);
        r3.setWasFound(true);

        merger.merge(0, r1);
        merger.merge(1, r2);
        merger.merge(2, r3);
        Tuple2<Integer, Reply> ret = merger.mergedReply();
        assertNull(ret.first);
        assertSame(r1, ret.second);
        assertThatErrorsMatch(new Error[] { e1 }, ret);
    }

    @Test
    public void returnUpdateDocumentReplyWhereDocWasFound() {
        UpdateDocumentReply r1 = new UpdateDocumentReply();
        UpdateDocumentReply r2 = new UpdateDocumentReply();
        UpdateDocumentReply r3 = new UpdateDocumentReply();
        r1.setWasFound(false);
        r2.setWasFound(true); // return first reply
        r3.setWasFound(true);

        merger.merge(0, r1);
        merger.merge(1, r2);
        merger.merge(2, r3);
        Tuple2<Integer, Reply> ret = merger.mergedReply();
        assertEquals(1, ret.first.intValue());
        assertSame(r2, ret.second);
    }

    @Test
    public void returnGetDocumentReplyWhereDocWasFound() {
        GetDocumentReply r1 = new GetDocumentReply(null);
        GetDocumentReply r2 = new GetDocumentReply(null);
        GetDocumentReply r3 = new GetDocumentReply(null);
        r2.setLastModified(12345L);

        merger.merge(0, r1);
        merger.merge(1, r2);
        merger.merge(2, r3);
        Tuple2<Integer, Reply> ret = merger.mergedReply();
        assertEquals(1, ret.first.intValue());
        assertSame(r2, ret.second);
    }

    @Test
    public void mergingZeroRepliesReturnsDefaultEmptyReply() {
        Tuple2<Integer, Reply> ret = merger.mergedReply();
        assertNull(ret.first);
        assertTrue(ret.second instanceof EmptyReply);
        assertThatErrorsMatch(new Error[]{}, ret);
    }

    private void assertThatErrorsMatch(Error[] errors, Tuple2<Integer, Reply> ret) {
        assertEquals(errors.length, ret.second.getNumErrors());
        for (int i = 0; i < ret.second.getNumErrors(); ++i) {
            assertEquals(errors[i].getCode(), ret.second.getError(i).getCode());
            assertEquals(errors[i].getMessage(), ret.second.getError(i).getMessage());
        }
    }

}