1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 É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
.Collections
;
28 import java
.util
.LinkedList
;
30 import org
.eclipse
.jdt
.annotation
.NonNull
;
31 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.synchronization
.SyncAlgorithmFullyIncremental
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.event
.matching
.TmfEventDependency
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.ITmfTimestampTransform
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.SynchronizationAlgorithm
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.SynchronizationAlgorithm
.SyncQuality
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.SynchronizationAlgorithmFactory
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.TimestampTransformFactory
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
40 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.event
.TmfSyncEventStub
;
41 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
42 import org
.junit
.Before
;
43 import org
.junit
.Test
;
46 * Tests for {@link SynchronizationAlgorithm} and its descendants
48 * @author Geneviève Bastien
50 @SuppressWarnings("nls")
51 public class SyncTest
{
53 private TmfTraceStub t1
, t2
;
54 private @NonNull Collection
<ITmfTrace
> fTraces
= Collections
.EMPTY_LIST
;
57 * Initializing the traces
61 t1
= new TmfTraceStub();
63 t2
= new TmfTraceStub();
66 Collection
<ITmfTrace
> traces
= new LinkedList
<>();
73 * Testing fully incremental algorithm with communication between the two
77 public void testFullyIncremental() {
79 SynchronizationAlgorithm syncAlgo
= SynchronizationAlgorithmFactory
.getFullyIncrementalAlgorithm();
81 syncAlgo
.init(fTraces
);
83 assertEquals(SyncQuality
.ABSENT
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
84 addSyncMatch(syncAlgo
, t2
, 1, t1
, 1);
85 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo
.toString());
86 assertEquals(SyncQuality
.INCOMPLETE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
88 addSyncMatch(syncAlgo
, t1
, 1, t2
, 3);
89 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo
.toString());
90 assertEquals(SyncQuality
.INCOMPLETE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
92 addSyncMatch(syncAlgo
, t2
, 2, t1
, 3);
93 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0.5 ]]", syncAlgo
.toString());
94 assertEquals(SyncQuality
.APPROXIMATE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
96 addSyncMatch(syncAlgo
, t1
, 3, t2
, 5);
97 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 0.75 beta 1.25 ]]", syncAlgo
.toString());
98 assertEquals(SyncQuality
.ACCURATE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
100 addSyncMatch(syncAlgo
, t1
, 4, t2
, 8);
101 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 0.75 beta 1.25 ]]", syncAlgo
.toString());
102 assertEquals(SyncQuality
.ACCURATE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
104 addSyncMatch(syncAlgo
, t2
, 4, t1
, 5);
105 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1.125 beta 0.875 ]]", syncAlgo
.toString());
106 assertEquals(SyncQuality
.ACCURATE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
108 addSyncMatch(syncAlgo
, t2
, 4, t1
, 6);
109 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1.125 beta 0.875 ]]", syncAlgo
.toString());
110 assertEquals(SyncQuality
.ACCURATE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
112 addSyncMatch(syncAlgo
, t1
, 6, t2
, 7);
113 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 0.725 beta 1.275 ]]", syncAlgo
.toString());
114 assertEquals(SyncQuality
.ACCURATE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
116 ITmfTimestampTransform tt2
= syncAlgo
.getTimestampTransform(t2
);
117 ITmfTimestampTransform tt1
= syncAlgo
.getTimestampTransform(t1
);
119 assertEquals(syncAlgo
.getTimestampTransform(t1
.getHostId()), tt1
);
120 assertEquals(TimestampTransformFactory
.getDefaultTransform(), tt1
);
121 assertEquals(syncAlgo
.getTimestampTransform(t2
.getHostId()), tt2
);
123 /* Make the two hulls intersect */
124 addSyncMatch(syncAlgo
, t1
, 7, t2
, 4);
125 addSyncMatch(syncAlgo
, t2
, 7, t1
, 3);
126 assertEquals(SyncQuality
.FAIL
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
130 * Testing the fully incremental synchronization algorithm when
131 * communication goes in only one direction
134 public void testOneHull() {
136 SynchronizationAlgorithm syncAlgo
= SynchronizationAlgorithmFactory
.getFullyIncrementalAlgorithm();
138 syncAlgo
.init(fTraces
);
140 assertEquals(SyncQuality
.ABSENT
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
142 addSyncMatch(syncAlgo
, t1
, 1, t2
, 3);
143 assertEquals(SyncQuality
.INCOMPLETE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
145 addSyncMatch(syncAlgo
, t1
, 2, t2
, 5);
146 assertEquals(SyncQuality
.INCOMPLETE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
148 addSyncMatch(syncAlgo
, t1
, 3, t2
, 5);
149 assertEquals(SyncQuality
.INCOMPLETE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
151 addSyncMatch(syncAlgo
, t1
, 4, t2
, 7);
152 assertEquals(SyncQuality
.INCOMPLETE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
153 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo
.toString());
158 * Testing the fully incremental synchronization algorithm when all
159 * communication from trace1 to trace2 happens before all communication from
163 public void testDisjoint() {
165 SynchronizationAlgorithm syncAlgo
= SynchronizationAlgorithmFactory
.getFullyIncrementalAlgorithm();
167 syncAlgo
.init(fTraces
);
169 assertEquals(SyncQuality
.ABSENT
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
171 addSyncMatch(syncAlgo
, t1
, 1, t2
, 3);
172 assertEquals(SyncQuality
.INCOMPLETE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
174 addSyncMatch(syncAlgo
, t1
, 2, t2
, 5);
175 assertEquals(SyncQuality
.INCOMPLETE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
177 addSyncMatch(syncAlgo
, t1
, 3, t2
, 5);
178 assertEquals(SyncQuality
.INCOMPLETE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
180 addSyncMatch(syncAlgo
, t1
, 4, t2
, 7);
181 assertEquals(SyncQuality
.INCOMPLETE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
182 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo
.toString());
184 addSyncMatch(syncAlgo
, t2
, 7, t1
, 6);
185 assertEquals(SyncQuality
.APPROXIMATE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
187 addSyncMatch(syncAlgo
, t2
, 8, t1
, 6);
188 assertEquals(SyncQuality
.APPROXIMATE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
190 addSyncMatch(syncAlgo
, t2
, 10, t1
, 8);
191 assertEquals(SyncQuality
.APPROXIMATE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
192 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 2.5 ]]", syncAlgo
.toString());
195 private static void addSyncMatch(SynchronizationAlgorithm algo
, ITmfTrace sender
, long sendTs
, ITmfTrace receiver
, long receiveTs
) {
197 new TmfEventDependency(
198 new TmfSyncEventStub(sender
, new TmfTimestamp(sendTs
)),
199 new TmfSyncEventStub(receiver
, new TmfTimestamp(receiveTs
))
204 * Testing the serialization of the fully incremental synchronization
208 public void testFullyIncrementalSerialization() {
210 /* Do a run of synchronization and check the results */
211 SynchronizationAlgorithm syncAlgo
= SynchronizationAlgorithmFactory
.getFullyIncrementalAlgorithm();
213 syncAlgo
.init(fTraces
);
215 addSyncMatch(syncAlgo
, t2
, 1, t1
, 1);
216 addSyncMatch(syncAlgo
, t1
, 1, t2
, 3);
217 addSyncMatch(syncAlgo
, t2
, 2, t1
, 3);
218 addSyncMatch(syncAlgo
, t1
, 3, t2
, 5);
219 addSyncMatch(syncAlgo
, t1
, 4, t2
, 8);
220 addSyncMatch(syncAlgo
, t2
, 4, t1
, 5);
221 addSyncMatch(syncAlgo
, t2
, 4, t1
, 6);
222 addSyncMatch(syncAlgo
, t1
, 6, t2
, 7);
224 ITmfTimestampTransform tt2
= syncAlgo
.getTimestampTransform(t2
);
225 ITmfTimestampTransform tt1
= syncAlgo
.getTimestampTransform(t1
);
227 assertEquals(SyncQuality
.ACCURATE
, syncAlgo
.getSynchronizationQuality(t1
, t2
));
228 assertEquals(syncAlgo
.getTimestampTransform(t1
.getHostId()), tt1
);
229 assertEquals(TimestampTransformFactory
.getDefaultTransform(), tt1
);
230 assertEquals(syncAlgo
.getTimestampTransform(t2
.getHostId()), tt2
);
232 /* Serialize the object */
233 String filePath
= null;
235 File temp
= File
.createTempFile("serialSyncAlgo", ".tmp");
236 filePath
= temp
.getAbsolutePath();
237 } catch (IOException e
) {
238 fail("Could not create temporary file for serialization");
240 assertNotNull(filePath
);
242 try (FileOutputStream fileOut
= new FileOutputStream(filePath
);
243 ObjectOutputStream out
= new ObjectOutputStream(fileOut
);) {
244 out
.writeObject(syncAlgo
);
246 } catch (IOException e
) {
247 fail("Error serializing the synchronization algorithm " + e
.getMessage());
250 SynchronizationAlgorithm deserialAlgo
= null;
251 /* De-Serialize the object */
252 try (FileInputStream fileIn
= new FileInputStream(filePath
);
253 ObjectInputStream in
= new ObjectInputStream(fileIn
);) {
254 deserialAlgo
= (SynchronizationAlgorithm
) in
.readObject();
255 } catch (IOException
| ClassNotFoundException e
) {
256 fail("Error de-serializing the synchronization algorithm " + e
.getMessage());
259 /* Check that the deserialize algorithm is equivalent to original */
260 assertNotNull(deserialAlgo
);
261 assertTrue(deserialAlgo
instanceof SyncAlgorithmFullyIncremental
);
262 assertEquals(SyncQuality
.ACCURATE
, deserialAlgo
.getSynchronizationQuality(t1
, t2
));
263 assertEquals(tt1
, deserialAlgo
.getTimestampTransform(t1
));
264 assertEquals(tt2
, deserialAlgo
.getTimestampTransform(t2
));