2727import java .util .concurrent .atomic .AtomicInteger ;
2828import java .util .concurrent .atomic .AtomicLong ;
2929
30+ import org .apache .activemq .artemis .core .config .Configuration ;
31+ import org .apache .activemq .artemis .core .config .impl .ConfigurationImpl ;
3032import org .apache .activemq .artemis .core .io .IOCallback ;
3133import org .apache .activemq .artemis .core .io .OperationConsistencyLevel ;
34+ import org .apache .activemq .artemis .core .journal .Journal ;
35+ import org .apache .activemq .artemis .core .journal .impl .JournalImpl ;
3236import org .apache .activemq .artemis .core .paging .PagedMessage ;
3337import org .apache .activemq .artemis .core .persistence .OperationContext ;
3438import org .apache .activemq .artemis .core .persistence .StorageManager ;
39+ import org .apache .activemq .artemis .core .persistence .impl .journal .JournalStorageManager ;
3540import org .apache .activemq .artemis .core .persistence .impl .journal .OperationContextImpl ;
41+ import org .apache .activemq .artemis .core .server .JournalType ;
3642import org .apache .activemq .artemis .core .server .RouteContextList ;
3743import org .apache .activemq .artemis .core .transaction .Transaction ;
3844import org .apache .activemq .artemis .core .transaction .TransactionOperationAbstract ;
3945import org .apache .activemq .artemis .core .transaction .impl .TransactionImpl ;
4046import org .apache .activemq .artemis .tests .util .ArtemisTestCase ;
47+ import org .apache .activemq .artemis .utils .ExecutorFactory ;
4148import org .apache .activemq .artemis .utils .actors .OrderedExecutorFactory ;
49+ import org .apache .activemq .artemis .utils .critical .CriticalAnalyzer ;
4250import org .junit .jupiter .api .BeforeEach ;
4351import org .junit .jupiter .api .Test ;
4452import org .mockito .Mockito ;
5058import static org .junit .jupiter .api .Assertions .assertNotNull ;
5159import static org .junit .jupiter .api .Assertions .assertTrue ;
5260
53- public class PageSyncTimerUnitTest extends ArtemisTestCase {
61+ public class PageTimedWriterUnitTest extends ArtemisTestCase {
5462
5563 private static final Logger logger = LoggerFactory .getLogger (MethodHandles .lookup ().lookupClass ());
5664
5765 ScheduledExecutorService scheduledExecutorService ;
5866 ExecutorService executorService ;
5967 OrderedExecutorFactory executorFactory ;
6068 OperationContext context ;
61- StorageManager mockStorageManager ;
69+ JournalStorageManager journalStorageManager ;
6270
6371 PagingStoreImpl mockPageStore ;
6472
6573 CountDownLatch allowRunning ;
6674
6775 PageTimedWriter timer ;
6876
77+ Configuration configuration ;
78+
79+ Journal mockBindingsJournal ;
80+ Journal mockMessageJournal ;
81+
82+
83+ class MockableJournalStorageManager extends JournalStorageManager {
84+
85+ public MockableJournalStorageManager (Configuration config ,
86+ Journal bindingsJournal ,
87+ Journal messagesJournal ,
88+ ExecutorFactory executorFactory ,
89+ ExecutorFactory ioExecutors ) {
90+ super (config , Mockito .mock (CriticalAnalyzer .class ), executorFactory , ioExecutors );
91+ this .bindingsJournal = bindingsJournal ;
92+ this .messageJournal = messagesJournal ;
93+ }
94+
95+ @ Override
96+ public void start () throws Exception {
97+ super .start ();
98+ idGenerator .forceNextID (1 );
99+ }
100+
101+ @ Override
102+ protected void createDirectories () {
103+ // not creating any folders
104+ }
105+ }
106+
69107
70108 @ BeforeEach
71109 public void prepareTest () throws Exception {
110+ configuration = new ConfigurationImpl ();
111+ configuration .setJournalType (JournalType .NIO );
72112 scheduledExecutorService = Executors .newScheduledThreadPool (10 );
73113 executorService = Executors .newFixedThreadPool (10 );
74114 runAfter (scheduledExecutorService ::shutdownNow );
@@ -77,26 +117,18 @@ public void prepareTest() throws Exception {
77117 executorFactory = new OrderedExecutorFactory (executorService );
78118 context = OperationContextImpl .getContext (executorFactory );
79119 assertNotNull (context );
80- mockStorageManager = Mockito .mock (StorageManager .class );
81120
82- Mockito .doAnswer (i -> {
83- OperationContextImpl .getContext ().executeOnCompletion (i .getArgument (0 ));
84- return null ;
85- }).when (mockStorageManager ).afterCompleteOperations (Mockito .any (IOCallback .class ));
121+ mockBindingsJournal = Mockito .mock (Journal .class );
122+ mockMessageJournal = Mockito .mock (Journal .class );
86123
87- Mockito .doAnswer (i -> {
88- OperationContextImpl .getContext ().executeOnCompletion (i .getArgument (0 ));
89- return null ;
90- }).when (mockStorageManager ).afterCompleteOperations (Mockito .any (IOCallback .class ), Mockito .any ());
91-
92- AtomicLong nextInt = new AtomicLong (1L );
93- Mockito .when (mockStorageManager .generateID ()).then (l -> nextInt .incrementAndGet ());
124+ journalStorageManager = new MockableJournalStorageManager (configuration , mockBindingsJournal , mockMessageJournal , executorFactory , executorFactory );
125+ journalStorageManager .start ();
94126
95127 allowRunning = new CountDownLatch (1 );
96128
97129 mockPageStore = Mockito .mock (PagingStoreImpl .class );
98130
99- timer = new PageTimedWriter (mockStorageManager , mockPageStore , scheduledExecutorService , executorFactory .getExecutor (), 100 ) {
131+ timer = new PageTimedWriter (journalStorageManager , mockPageStore , scheduledExecutorService , executorFactory .getExecutor (), 100 ) {
100132 @ Override
101133 public void run () {
102134 try {
@@ -116,7 +148,7 @@ public void run() {
116148 // a test to validate if the Mocks are correctly setup
117149 @ Test
118150 public void testValidateMocks () throws Exception {
119- TransactionImpl tx = new TransactionImpl (mockStorageManager );
151+ TransactionImpl tx = new TransactionImpl (journalStorageManager );
120152 tx .setContainsPersistent ();
121153 AtomicInteger count = new AtomicInteger (0 );
122154 tx .addOperation (new TransactionOperationAbstract () {
@@ -130,7 +162,7 @@ public void afterCommit(Transaction tx) {
130162 assertEquals (1 , count .get (), "tx.commit is not correctly wired on mocking" );
131163
132164
133- mockStorageManager .afterCompleteOperations (new IOCallback () {
165+ journalStorageManager .afterCompleteOperations (new IOCallback () {
134166 @ Override
135167 public void done () {
136168 count .incrementAndGet ();
@@ -142,7 +174,7 @@ public void onError(int errorCode, String errorMessage) {
142174 }
143175 });
144176
145- mockStorageManager .afterCompleteOperations (new IOCallback () {
177+ journalStorageManager .afterCompleteOperations (new IOCallback () {
146178 @ Override
147179 public void done () {
148180 count .incrementAndGet ();
@@ -156,6 +188,10 @@ public void onError(int errorCode, String errorMessage) {
156188
157189 assertEquals (3 , count .get (), "afterCompletion is not correctly wired on mocking" );
158190
191+ long id = journalStorageManager .generateID ();
192+ long newID = journalStorageManager .generateID ();
193+ assertEquals (1L , newID - id );
194+
159195 }
160196
161197 @ Test
@@ -187,7 +223,7 @@ public void testIOCompletionWhileReplica() throws Exception {
187223
188224 AtomicBoolean replicated = new AtomicBoolean (true );
189225
190- Mockito .when (mockStorageManager .isReplicated ()).then (r -> replicated .get ());
226+ Mockito .when (journalStorageManager .isReplicated ()).then (r -> replicated .get ());
191227
192228 timer .addTask (context , Mockito .mock (PagedMessage .class ), null , Mockito .mock (RouteContextList .class ));
193229
@@ -224,7 +260,7 @@ public void testTXCompletion() throws Exception {
224260
225261 CountDownLatch latch = new CountDownLatch (1 );
226262
227- Transaction tx = new TransactionImpl (mockStorageManager , Integer .MAX_VALUE );
263+ Transaction tx = new TransactionImpl (journalStorageManager , Integer .MAX_VALUE );
228264 tx .setContainsPersistent ();
229265
230266 timer .addTask (context , Mockito .mock (PagedMessage .class ), tx , Mockito .mock (RouteContextList .class ));
0 commit comments