tmf: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / synchronization / SyncTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Geneviève Bastien - Initial implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.core.tests.synchronization;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assert.fail;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.ObjectInputStream;
25 import java.io.ObjectOutputStream;
26 import java.util.Collection;
27 import java.util.LinkedList;
28
29 import org.eclipse.tracecompass.internal.tmf.core.synchronization.SyncAlgorithmFullyIncremental;
30 import org.eclipse.tracecompass.tmf.core.event.matching.TmfEventDependency;
31 import org.eclipse.tracecompass.tmf.core.synchronization.ITmfTimestampTransform;
32 import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithm;
33 import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithm.SyncQuality;
34 import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithmFactory;
35 import org.eclipse.tracecompass.tmf.core.synchronization.TimestampTransformFactory;
36 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
37 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
38 import org.eclipse.tracecompass.tmf.tests.stubs.event.TmfSyncEventStub;
39 import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub;
40 import org.junit.Before;
41 import org.junit.Test;
42
43 /**
44 * Tests for {@link SynchronizationAlgorithm} and its descendants
45 *
46 * @author Geneviève Bastien
47 */
48 @SuppressWarnings("nls")
49 public class SyncTest {
50
51 private TmfTraceStub t1, t2;
52 private Collection<ITmfTrace> fTraces;
53
54 /**
55 * Initializing the traces
56 */
57 @Before
58 public void init() {
59 t1 = new TmfTraceStub();
60 t1.init("t1");
61 t2 = new TmfTraceStub();
62 t2.init("t2");
63
64 Collection<ITmfTrace> traces = new LinkedList<>();
65 traces.add(t1);
66 traces.add(t2);
67 fTraces = traces;
68 }
69
70 /**
71 * Testing fully incremental algorithm with communication between the two
72 * traces
73 */
74 @Test
75 public void testFullyIncremental() {
76
77 SynchronizationAlgorithm syncAlgo = SynchronizationAlgorithmFactory.getFullyIncrementalAlgorithm();
78
79 syncAlgo.init(fTraces);
80
81 assertEquals(SyncQuality.ABSENT, syncAlgo.getSynchronizationQuality(t1, t2));
82 addSyncMatch(syncAlgo, t2, 1, t1, 1);
83 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString());
84 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
85
86 addSyncMatch(syncAlgo, t1, 1, t2, 3);
87 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString());
88 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
89
90 addSyncMatch(syncAlgo, t2, 2, t1, 3);
91 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0.5 ]]", syncAlgo.toString());
92 assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2));
93
94 addSyncMatch(syncAlgo, t1, 3, t2, 5);
95 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 0.75 beta 1.25 ]]", syncAlgo.toString());
96 assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
97
98 addSyncMatch(syncAlgo, t1, 4, t2, 8);
99 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 0.75 beta 1.25 ]]", syncAlgo.toString());
100 assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
101
102 addSyncMatch(syncAlgo, t2, 4, t1, 5);
103 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1.125 beta 0.875 ]]", syncAlgo.toString());
104 assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
105
106 addSyncMatch(syncAlgo, t2, 4, t1, 6);
107 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1.125 beta 0.875 ]]", syncAlgo.toString());
108 assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
109
110 addSyncMatch(syncAlgo, t1, 6, t2, 7);
111 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 0.725 beta 1.275 ]]", syncAlgo.toString());
112 assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
113
114 ITmfTimestampTransform tt2 = syncAlgo.getTimestampTransform(t2);
115 ITmfTimestampTransform tt1 = syncAlgo.getTimestampTransform(t1);
116
117 assertEquals(syncAlgo.getTimestampTransform(t1.getHostId()), tt1);
118 assertEquals(TimestampTransformFactory.getDefaultTransform(), tt1);
119 assertEquals(syncAlgo.getTimestampTransform(t2.getHostId()), tt2);
120
121 /* Make the two hulls intersect */
122 addSyncMatch(syncAlgo, t1, 7, t2, 4);
123 addSyncMatch(syncAlgo, t2, 7, t1, 3);
124 assertEquals(SyncQuality.FAIL, syncAlgo.getSynchronizationQuality(t1, t2));
125 }
126
127 /**
128 * Testing the fully incremental synchronization algorithm when
129 * communication goes in only one direction
130 */
131 @Test
132 public void testOneHull() {
133
134 SynchronizationAlgorithm syncAlgo = SynchronizationAlgorithmFactory.getFullyIncrementalAlgorithm();
135
136 syncAlgo.init(fTraces);
137
138 assertEquals(SyncQuality.ABSENT, syncAlgo.getSynchronizationQuality(t1, t2));
139
140 addSyncMatch(syncAlgo, t1, 1, t2, 3);
141 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
142
143 addSyncMatch(syncAlgo, t1, 2, t2, 5);
144 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
145
146 addSyncMatch(syncAlgo, t1, 3, t2, 5);
147 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
148
149 addSyncMatch(syncAlgo, t1, 4, t2, 7);
150 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
151 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString());
152
153 }
154
155 /**
156 * Testing the fully incremental synchronization algorithm when all
157 * communication from trace1 to trace2 happens before all communication from
158 * trace2 to trace1
159 */
160 @Test
161 public void testDisjoint() {
162
163 SynchronizationAlgorithm syncAlgo = SynchronizationAlgorithmFactory.getFullyIncrementalAlgorithm();
164
165 syncAlgo.init(fTraces);
166
167 assertEquals(SyncQuality.ABSENT, syncAlgo.getSynchronizationQuality(t1, t2));
168
169 addSyncMatch(syncAlgo, t1, 1, t2, 3);
170 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
171
172 addSyncMatch(syncAlgo, t1, 2, t2, 5);
173 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
174
175 addSyncMatch(syncAlgo, t1, 3, t2, 5);
176 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
177
178 addSyncMatch(syncAlgo, t1, 4, t2, 7);
179 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
180 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString());
181
182 addSyncMatch(syncAlgo, t2, 7, t1, 6);
183 assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2));
184
185 addSyncMatch(syncAlgo, t2, 8, t1, 6);
186 assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2));
187
188 addSyncMatch(syncAlgo, t2, 10, t1, 8);
189 assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2));
190 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 2.5 ]]", syncAlgo.toString());
191 }
192
193 private static void addSyncMatch(SynchronizationAlgorithm algo, ITmfTrace sender, long sendTs, ITmfTrace receiver, long receiveTs) {
194 algo.addMatch(
195 new TmfEventDependency(
196 new TmfSyncEventStub(sender, new TmfTimestamp(sendTs)),
197 new TmfSyncEventStub(receiver, new TmfTimestamp(receiveTs))
198 ));
199 }
200
201 /**
202 * Testing the serialization of the fully incremental synchronization
203 * algorithm
204 */
205 @Test
206 public void testFullyIncrementalSerialization() {
207
208 /* Do a run of synchronization and check the results */
209 SynchronizationAlgorithm syncAlgo = SynchronizationAlgorithmFactory.getFullyIncrementalAlgorithm();
210
211 syncAlgo.init(fTraces);
212
213 addSyncMatch(syncAlgo, t2, 1, t1, 1);
214 addSyncMatch(syncAlgo, t1, 1, t2, 3);
215 addSyncMatch(syncAlgo, t2, 2, t1, 3);
216 addSyncMatch(syncAlgo, t1, 3, t2, 5);
217 addSyncMatch(syncAlgo, t1, 4, t2, 8);
218 addSyncMatch(syncAlgo, t2, 4, t1, 5);
219 addSyncMatch(syncAlgo, t2, 4, t1, 6);
220 addSyncMatch(syncAlgo, t1, 6, t2, 7);
221
222 ITmfTimestampTransform tt2 = syncAlgo.getTimestampTransform(t2);
223 ITmfTimestampTransform tt1 = syncAlgo.getTimestampTransform(t1);
224
225 assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
226 assertEquals(syncAlgo.getTimestampTransform(t1.getHostId()), tt1);
227 assertEquals(TimestampTransformFactory.getDefaultTransform(), tt1);
228 assertEquals(syncAlgo.getTimestampTransform(t2.getHostId()), tt2);
229
230 /* Serialize the object */
231 String filePath = null;
232 try {
233 File temp = File.createTempFile("serialSyncAlgo", ".tmp");
234 filePath = temp.getAbsolutePath();
235 } catch (IOException e) {
236 fail("Could not create temporary file for serialization");
237 }
238 assertNotNull(filePath);
239
240 try (FileOutputStream fileOut = new FileOutputStream(filePath);
241 ObjectOutputStream out = new ObjectOutputStream(fileOut);) {
242 out.writeObject(syncAlgo);
243
244 } catch (IOException e) {
245 fail("Error serializing the synchronization algorithm " + e.getMessage());
246 }
247
248 SynchronizationAlgorithm deserialAlgo = null;
249 /* De-Serialize the object */
250 try (FileInputStream fileIn = new FileInputStream(filePath);
251 ObjectInputStream in = new ObjectInputStream(fileIn);) {
252 deserialAlgo = (SynchronizationAlgorithm) in.readObject();
253 } catch (IOException | ClassNotFoundException e) {
254 fail("Error de-serializing the synchronization algorithm " + e.getMessage());
255 }
256
257 /* Check that the deserialize algorithm is equivalent to original */
258 assertNotNull(deserialAlgo);
259 assertTrue(deserialAlgo instanceof SyncAlgorithmFullyIncremental);
260 assertEquals(SyncQuality.ACCURATE, deserialAlgo.getSynchronizationQuality(t1, t2));
261 assertEquals(tt1, deserialAlgo.getTimestampTransform(t1));
262 assertEquals(tt2, deserialAlgo.getTimestampTransform(t2));
263
264 }
265
266 }
This page took 0.037042 seconds and 6 git commands to generate.