blob: 0e84ba04647e0a3efaaf8c243ffb5001135e7b12 (
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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.docproc;
import com.yahoo.document.DataType;
import com.yahoo.document.Document;
import com.yahoo.document.DocumentId;
import com.yahoo.document.DocumentOperation;
import com.yahoo.document.DocumentPut;
import com.yahoo.document.DocumentType;
import com.yahoo.document.datatypes.StringFieldValue;
/**
* Tests a document processing where some processings fail permanently
*
* @author <a href="mailto:einarmr@yahoo-inc.com">Einar M. R. Rosenvinge</a>
*/
public class FailingPermanentlyDocumentProcessingTestCase extends junit.framework.TestCase {
public FailingPermanentlyDocumentProcessingTestCase(String name) {
super(name);
}
/**
* Tests chaining of some processors, and execution of the processors
* on some documents
*/
public void testFailingProcessing() {
// Set up service programmatically
DocprocService service = new DocprocService("failing-permanently");
DocumentProcessor first = new SettingValueProcessor("done 1");
DocumentProcessor second = new FailingProcessor("done 2");
DocumentProcessor third = new SettingValueProcessor("done 3");
service.setCallStack(new CallStack().addLast(first).addLast(second).addLast(third));
service.setInService(true);
assertProcessingWorks(service);
}
protected void assertProcessingWorks(DocprocService service) {
// Create documents
DocumentType type = new DocumentType("test");
type.addField("test", DataType.STRING);
DocumentPut put1 = new DocumentPut(type, new DocumentId("doc:permanentfailure:test:1"));
DocumentPut put2 = new DocumentPut(type, new DocumentId("doc:permanentfailure:test:2"));
DocumentPut put3 = new DocumentPut(type, new DocumentId("doc:permanentfailure:test:3"));
// Process them
service.process(put1);
service.process(put2);
service.process(put3);
while (service.doWork()) {}
// Verify
assertEquals(new StringFieldValue("done 3"), put1.getDocument().getFieldValue("test"));
assertEquals(new StringFieldValue("done 2"), put2.getDocument().getFieldValue("test")); // Due to PERMANENT_FAILURE in 2
assertNull(put3.getDocument().getFieldValue("test")); //service is disabled now
assertFalse(service.isInService());
service.setInService(true);
while (service.doWork()) {}
assertEquals(new StringFieldValue("done 3"), put3.getDocument().getFieldValue("test"));
assertTrue(service.isInService());
}
public static class SettingValueProcessor extends DocumentProcessor {
private String value;
public SettingValueProcessor(String value) {
this.value = value;
}
@Override
public Progress process(Processing processing) {
for (DocumentOperation op : processing.getDocumentOperations()) {
((DocumentPut)op).getDocument().setFieldValue("test", value);
}
return Progress.DONE;
}
}
public static class FailingProcessor extends SettingValueProcessor {
public FailingProcessor(String name) {
super(name);
}
@Override
public Progress process(Processing processing) {
super.process(processing);
for (DocumentOperation op : processing.getDocumentOperations()) {
if (op.getId().toString().equals("doc:permanentfailure:test:2")) {
return DocumentProcessor.Progress.PERMANENT_FAILURE;
}
}
return DocumentProcessor.Progress.DONE;
}
}
}
|