Restore QJavalinImplementation to original state after testHotSwap

This commit is contained in:
2023-08-15 11:38:46 -05:00
parent db0b434e52
commit 2577bbeb37

View File

@ -31,6 +31,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Function; import java.util.function.Function;
import com.kingsrook.qqq.backend.core.exceptions.QInstanceValidationException;
import com.kingsrook.qqq.backend.core.model.actions.reporting.ReportFormat; import com.kingsrook.qqq.backend.core.model.actions.reporting.ReportFormat;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.WidgetType; import com.kingsrook.qqq.backend.core.model.dashboard.widgets.WidgetType;
import com.kingsrook.qqq.backend.core.model.metadata.QBackendMetaData; import com.kingsrook.qqq.backend.core.model.metadata.QBackendMetaData;
@ -874,86 +875,98 @@ class QJavalinImplementationTest extends QJavalinTestBase
** **
*******************************************************************************/ *******************************************************************************/
@Test @Test
void testHotSwap() void testHotSwap() throws QInstanceValidationException
{ {
Function<String, QInstance> makeNewInstanceWithBackendName = (backendName) -> try
{ {
QInstance newInstance = new QInstance(); Function<String, QInstance> makeNewInstanceWithBackendName = (backendName) ->
newInstance.addBackend(new QBackendMetaData().withName(backendName).withBackendType("mock"));
if(!"invalid".equals(backendName))
{ {
newInstance.addTable(new QTableMetaData() QInstance newInstance = new QInstance();
.withName("newTable") newInstance.addBackend(new QBackendMetaData().withName(backendName).withBackendType("mock"));
.withBackendName(backendName)
.withField(new QFieldMetaData("newField", QFieldType.INTEGER))
.withPrimaryKeyField("newField")
);
}
return (newInstance); if(!"invalid".equals(backendName))
}; {
newInstance.addTable(new QTableMetaData()
.withName("newTable")
.withBackendName(backendName)
.withField(new QFieldMetaData("newField", QFieldType.INTEGER))
.withPrimaryKeyField("newField")
);
}
QJavalinImplementation.setQInstanceHotSwapSupplier(() -> makeNewInstanceWithBackendName.apply("newBackend")); return (newInstance);
};
///////////////////////////////////////////////////////////////////////////////// QJavalinImplementation.setQInstanceHotSwapSupplier(() -> makeNewInstanceWithBackendName.apply("newBackend"));
// make sure before a hot-swap, that the instance doesn't have our new backend //
/////////////////////////////////////////////////////////////////////////////////
assertNull(QJavalinImplementation.qInstance.getBackend("newBackend"));
/////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
// do a hot-swap, make sure the new backend is there // // make sure before a hot-swap, that the instance doesn't have our new backend //
/////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
QJavalinImplementation.hotSwapQInstance(null); assertNull(QJavalinImplementation.qInstance.getBackend("newBackend"));
assertNotNull(QJavalinImplementation.qInstance.getBackend("newBackend"));
//////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////
// now change to make a different backend - try to swap again - but the newer backend shouldn't be there, // // do a hot-swap, make sure the new backend is there //
// because the millis-between-hot-swaps won't have passed // ///////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////// QJavalinImplementation.hotSwapQInstance(null);
QJavalinImplementation.setQInstanceHotSwapSupplier(() -> makeNewInstanceWithBackendName.apply("newerBackend")); assertNotNull(QJavalinImplementation.qInstance.getBackend("newBackend"));
QJavalinImplementation.hotSwapQInstance(null);
assertNull(QJavalinImplementation.qInstance.getBackend("newerBackend"));
//////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////
// set the sleep threshold to 1 milli, sleep for 2, and then assert that we do swap again // // now change to make a different backend - try to swap again - but the newer backend shouldn't be there, //
//////////////////////////////////////////////////////////////////////////////////////////// // because the millis-between-hot-swaps won't have passed //
QJavalinImplementation.setMillisBetweenHotSwaps(1); ////////////////////////////////////////////////////////////////////////////////////////////////////////////
SleepUtils.sleep(2, TimeUnit.MILLISECONDS); QJavalinImplementation.setQInstanceHotSwapSupplier(() -> makeNewInstanceWithBackendName.apply("newerBackend"));
QJavalinImplementation.hotSwapQInstance(null);
assertNull(QJavalinImplementation.qInstance.getBackend("newerBackend"));
QJavalinImplementation.setQInstanceHotSwapSupplier(() -> makeNewInstanceWithBackendName.apply("newerBackend")); ////////////////////////////////////////////////////////////////////////////////////////////
QJavalinImplementation.hotSwapQInstance(null); // set the sleep threshold to 1 milli, sleep for 2, and then assert that we do swap again //
assertNotNull(QJavalinImplementation.qInstance.getBackend("newerBackend")); ////////////////////////////////////////////////////////////////////////////////////////////
QJavalinImplementation.setMillisBetweenHotSwaps(1);
SleepUtils.sleep(2, TimeUnit.MILLISECONDS);
//////////////////////////////////////////////////////////// QJavalinImplementation.setQInstanceHotSwapSupplier(() -> makeNewInstanceWithBackendName.apply("newerBackend"));
// assert that an invalid instance doesn't get swapped in // QJavalinImplementation.hotSwapQInstance(null);
// e.g., "newerBackend" still exists // assertNotNull(QJavalinImplementation.qInstance.getBackend("newerBackend"));
////////////////////////////////////////////////////////////
SleepUtils.sleep(2, TimeUnit.MILLISECONDS);
QJavalinImplementation.setQInstanceHotSwapSupplier(() -> makeNewInstanceWithBackendName.apply("invalid"));
QJavalinImplementation.hotSwapQInstance(null);
assertNotNull(QJavalinImplementation.qInstance.getBackend("newerBackend"));
/////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// assert that if the supplier throws, we don't swap // // assert that an invalid instance doesn't get swapped in //
// e.g., "newerBackend" still exists // // e.g., "newerBackend" still exists //
/////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
SleepUtils.sleep(2, TimeUnit.MILLISECONDS); SleepUtils.sleep(2, TimeUnit.MILLISECONDS);
QJavalinImplementation.setQInstanceHotSwapSupplier(() -> QJavalinImplementation.setQInstanceHotSwapSupplier(() -> makeNewInstanceWithBackendName.apply("invalid"));
QJavalinImplementation.hotSwapQInstance(null);
assertNotNull(QJavalinImplementation.qInstance.getBackend("newerBackend"));
///////////////////////////////////////////////////////
// assert that if the supplier throws, we don't swap //
// e.g., "newerBackend" still exists //
///////////////////////////////////////////////////////
SleepUtils.sleep(2, TimeUnit.MILLISECONDS);
QJavalinImplementation.setQInstanceHotSwapSupplier(() ->
{
throw new RuntimeException("oops");
});
QJavalinImplementation.hotSwapQInstance(null);
assertNotNull(QJavalinImplementation.qInstance.getBackend("newerBackend"));
/////////////////////////////////////////////////////////////
// assert that if the supplier returns null, we don't swap //
// e.g., "newerBackend" still exists //
/////////////////////////////////////////////////////////////
SleepUtils.sleep(2, TimeUnit.MILLISECONDS);
QJavalinImplementation.setQInstanceHotSwapSupplier(() -> null);
QJavalinImplementation.hotSwapQInstance(null);
assertNotNull(QJavalinImplementation.qInstance.getBackend("newerBackend"));
}
finally
{ {
throw new RuntimeException("oops"); ////////////////////////////////////////////////////////////
}); // restore things to how they used to be, for other tests //
QJavalinImplementation.hotSwapQInstance(null); ////////////////////////////////////////////////////////////
assertNotNull(QJavalinImplementation.qInstance.getBackend("newerBackend")); QInstance qInstance = TestUtils.defineInstance();
QJavalinImplementation.setQInstanceHotSwapSupplier(null);
///////////////////////////////////////////////////////////// restartServerWithInstance(qInstance);
// assert that if the supplier returns null, we don't swap // }
// e.g., "newerBackend" still exists //
/////////////////////////////////////////////////////////////
SleepUtils.sleep(2, TimeUnit.MILLISECONDS);
QJavalinImplementation.setQInstanceHotSwapSupplier(() -> null);
QJavalinImplementation.hotSwapQInstance(null);
assertNotNull(QJavalinImplementation.qInstance.getBackend("newerBackend"));
} }
} }