1 /*******************************************************************************
2 * Copyright (c) 2013 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.synchronization
;
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
;
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
;
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
;
44 * Tests for {@link SynchronizationAlgorithm} and its descendants
46 * @author Geneviève Bastien
48 @SuppressWarnings("nls")
49 public class SyncTest
{
51 private TmfTraceStub t1
, t2
;
52 private Collection
<ITmfTrace
> fTraces
;
55 * Initializing the traces
59 t1
= new TmfTraceStub();
61 t2
= new TmfTraceStub();
64 Collection
<ITmfTrace
> traces
= new LinkedList
<>();
71 * Testing fully incremental algorithm with communication between the two
75 public void testFullyIncremental() {
77 SynchronizationAlgorithm syncAlgo
= SynchronizationAlgorithmFactory
.getFullyIncrementalAlgorithm();
79 syncAlgo
.init(fTraces
);
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
114 ITmfTimestampTransform tt2
= syncAlgo
.getTimestampTransform(t2
);
115 ITmfTimestampTransform tt1
= syncAlgo
.getTimestampTransform(t1
);
117 assertEquals(syncAlgo
.getTimestampTransform(t1
.getHostId()), tt1
);
118 assertEquals(TimestampTransformFactory
.getDefaultTransform(), tt1
);
119 assertEquals(syncAlgo
.getTimestampTransform(t2
.getHostId()), tt2
);
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
));
128 * Testing the fully incremental synchronization algorithm when
129 * communication goes in only one direction
132 public void testOneHull() {
134 SynchronizationAlgorithm syncAlgo
= SynchronizationAlgorithmFactory
.getFullyIncrementalAlgorithm();
136 syncAlgo
.init(fTraces
);
138 assertEquals(SyncQuality
.ABSENT
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
140 addSyncMatch(syncAlgo
, t1
, 1, t2
, 3);
141 assertEquals(SyncQuality
.INCOMPLETE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
143 addSyncMatch(syncAlgo
, t1
, 2, t2
, 5);
144 assertEquals(SyncQuality
.INCOMPLETE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
146 addSyncMatch(syncAlgo
, t1
, 3, t2
, 5);
147 assertEquals(SyncQuality
.INCOMPLETE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
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());
156 * Testing the fully incremental synchronization algorithm when all
157 * communication from trace1 to trace2 happens before all communication from
161 public void testDisjoint() {
163 SynchronizationAlgorithm syncAlgo
= SynchronizationAlgorithmFactory
.getFullyIncrementalAlgorithm();
165 syncAlgo
.init(fTraces
);
167 assertEquals(SyncQuality
.ABSENT
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
169 addSyncMatch(syncAlgo
, t1
, 1, t2
, 3);
170 assertEquals(SyncQuality
.INCOMPLETE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
172 addSyncMatch(syncAlgo
, t1
, 2, t2
, 5);
173 assertEquals(SyncQuality
.INCOMPLETE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
175 addSyncMatch(syncAlgo
, t1
, 3, t2
, 5);
176 assertEquals(SyncQuality
.INCOMPLETE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
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());
182 addSyncMatch(syncAlgo
, t2
, 7, t1
, 6);
183 assertEquals(SyncQuality
.APPROXIMATE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
185 addSyncMatch(syncAlgo
, t2
, 8, t1
, 6);
186 assertEquals(SyncQuality
.APPROXIMATE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
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());
193 private static void addSyncMatch(SynchronizationAlgorithm algo
, ITmfTrace sender
, long sendTs
, ITmfTrace receiver
, long receiveTs
) {
195 new TmfEventDependency(
196 new TmfSyncEventStub(sender
, new TmfTimestamp(sendTs
)),
197 new TmfSyncEventStub(receiver
, new TmfTimestamp(receiveTs
))
202 * Testing the serialization of the fully incremental synchronization
206 public void testFullyIncrementalSerialization() {
208 /* Do a run of synchronization and check the results */
209 SynchronizationAlgorithm syncAlgo
= SynchronizationAlgorithmFactory
.getFullyIncrementalAlgorithm();
211 syncAlgo
.init(fTraces
);
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);
222 ITmfTimestampTransform tt2
= syncAlgo
.getTimestampTransform(t2
);
223 ITmfTimestampTransform tt1
= syncAlgo
.getTimestampTransform(t1
);
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
);
230 /* Serialize the object */
231 String filePath
= null;
233 File temp
= File
.createTempFile("serialSyncAlgo", ".tmp");
234 filePath
= temp
.getAbsolutePath();
235 } catch (IOException e
) {
236 fail("Could not create temporary file for serialization");
238 assertNotNull(filePath
);
240 try (FileOutputStream fileOut
= new FileOutputStream(filePath
);
241 ObjectOutputStream out
= new ObjectOutputStream(fileOut
);) {
242 out
.writeObject(syncAlgo
);
244 } catch (IOException e
) {
245 fail("Error serializing the synchronization algorithm " + e
.getMessage());
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());
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
));