-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ActivityCorrelator behavior (#2254)
- Loading branch information
1 parent
e56deb1
commit df3c7b3
Showing
2 changed files
with
94 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/test/java/com/microsoft/sqlserver/jdbc/ActivityIDTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made | ||
* available under the terms of the MIT License. See the LICENSE file in the project root for more information. | ||
*/ | ||
package com.microsoft.sqlserver.jdbc; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.runner.JUnitPlatform; | ||
import org.junit.runner.RunWith; | ||
|
||
import com.microsoft.sqlserver.testframework.AbstractTest; | ||
|
||
|
||
@RunWith(JUnitPlatform.class) | ||
public class ActivityIDTest extends AbstractTest { | ||
|
||
@Test | ||
public void testActivityID() throws Exception { | ||
int numThreads = 20; | ||
List<UUID> usedIds = new ArrayList<UUID>(numThreads); | ||
|
||
for (int i = 0; i < numThreads; i++) { | ||
MyThread t = new MyThread(usedIds); | ||
t.start(); | ||
t.join(); | ||
usedIds.add(t.getUUID()); | ||
} | ||
} | ||
|
||
public class MyThread extends Thread { | ||
|
||
public MyThread(List<UUID> usedIdsIn) { | ||
usedIds = usedIdsIn; | ||
} | ||
|
||
private List<UUID> usedIds; | ||
private ActivityId id; | ||
|
||
public UUID getUUID() { | ||
return id.getId(); | ||
} | ||
|
||
public void run() { | ||
id = ActivityCorrelator.getCurrent(); | ||
UUID uuid = id.getId(); | ||
assertFalse("UUID should be unique across threads.", usedIds.contains(id.getId())); | ||
assertEquals(1L, id.getSequence(), "First sequence should be 1."); | ||
id = ActivityCorrelator.getNext(); | ||
assertEquals(uuid, id.getId(), "UUID should remain the same for the same thread."); | ||
assertEquals(2L, id.getSequence(), "Second sequence should be 2."); | ||
id = ActivityCorrelator.getNext(); | ||
assertEquals(3L, id.getSequence(), "Third sequence should be 3."); | ||
assertEquals(uuid, id.getId(), "UUID should remain the same for the same thread."); | ||
} | ||
} | ||
} |