def test_bad_input_message_federate_message(): broker = createBroker(1) mFed1, fedinfo = createMessageFederate(1, "test") ept1 = h.helicsFederateRegisterGlobalEndpoint(mFed1, "ept1", "") with pt.raises(h.HelicsException): h.helicsFederateRegisterGlobalEndpoint(mFed1, "ept1", "") h.helicsFederateEnterExecutingMode(mFed1) h.helicsEndpointSetDefaultDestination(ept1, "ept1") mess0 = h.helicsEndpointGetMessage(ept1) mess0 = h.helicsFederateGetMessage(mFed1) with pt.raises(h.HelicsException): h.helicsEndpointSendMessage(ept1, mess0) h.helicsFederateRequestNextStep(mFed1) cnt = h.helicsEndpointPendingMessages(ept1) assert cnt == 0 h.helicsFederateFinalize(mFed1) # @test_throws h.HELICSErrorInvalidFunctionCall with pt.raises(h.HelicsException): h.helicsEndpointSendMessage(ept1, mess0) destroyFederate(mFed1, fedinfo) destroyBroker(broker)
def test_bad_input_tests_raw_tests(): broker = createBroker(1) vFed1, fedinfo = createValueFederate(1, "test") pubid = h.helicsFederateRegisterPublication(vFed1, "pub1", h.HELICS_DATA_TYPE_RAW, "") subid = h.helicsFederateRegisterGlobalInput(vFed1, "inp1", h.HELICS_DATA_TYPE_RAW, "") h.helicsPublicationAddTarget(pubid, "inp1") h.helicsFederateSetTimeProperty(vFed1, h.HELICS_PROPERTY_TIME_PERIOD, 1.0) h.helicsFederateEnterExecutingMode(vFed1) h.helicsPublicationPublishRaw(pubid, b"hello world") h.helicsFederateRequestNextStep(vFed1) s = h.helicsInputGetRawValue(subid) assert s == b"hello world" h.helicsPublicationPublishDouble(pubid, 27) h.helicsFederateRequestNextStep(vFed1) s = h.helicsInputGetComplex(subid) assert complex(*s) != 27 + 0j h.helicsFederateFinalize(vFed1) t, res = h.helicsFederateRequestTimeIterative( vFed1, 1.0, h.HELICS_ITERATION_REQUEST_NO_ITERATION) assert res == h.HELICS_ITERATION_RESULT_HALTED destroyFederate(vFed1, fedinfo) destroyBroker(broker)
def test_bad_input_duplicate_publication_and_input_pathways(): broker = createBroker(1) vFed1, fedinfo = createValueFederate(1, "fed0") pubid = h.helicsFederateRegisterTypePublication(vFed1, "pub1", "string", "") # @test_throws h.HELICSErrorRegistrationFailure with pt.raises(h.HelicsException): pubid2 = h.helicsFederateRegisterTypePublication( vFed1, "pub1", "string", "") subid = h.helicsFederateRegisterGlobalTypeInput(vFed1, "inp1", "string", "") # @test_throws h.HELICSErrorRegistrationFailure with pt.raises(h.HelicsException): subid2 = h.helicsFederateRegisterGlobalTypeInput( vFed1, "inp1", "string", "") # @test_throws h.HELICSErrorInvalidObject ept = h.helicsFederateRegisterEndpoint(vFed1, "ept1", "") h.helicsInputAddTarget(subid, "Testfed0/pub1") h.helicsFederateSetTimeProperty(vFed1, h.HELICS_PROPERTY_TIME_PERIOD, 1.0) h.helicsFederateEnterExecutingMode(vFed1) h.helicsPublicationPublishDouble(pubid, 27.0) h.helicsFederateRequestNextStep(vFed1) str = h.helicsInputGetString(subid) assert str[0] == "2" assert str[1] == "7" messages = h.helicsFederatePendingMessages(vFed1) assert messages == 0 h.helicsFederateFinalize(vFed1) destroyFederate(vFed1, fedinfo) destroyBroker(broker)
def test_logging_api(): fi = h.helicsCreateFederateInfo() broker = h.helicsCreateBroker("zmq", "broker", "--federates 1 --loglevel 1") h.helicsFederateInfoSetCoreInitString(fi, "--federates 1") h.helicsFederateInfoSetIntegerProperty(fi, h.HELICS_PROPERTY_INT_LOG_LEVEL, 5) fed = h.helicsCreateValueFederate("test1", fi) userdata = UserData(5) handle = h.ffi.new_handle(userdata) h.helicsFederateSetLoggingCallback(fed, logger, handle) h.helicsFederateEnterExecutingMode(fed) h.helicsFederateLogInfoMessage(fed, "test MEXAGE") h.helicsFederateRequestNextStep(fed) h.helicsFederateLogInfoMessage(fed, "test MEXAGE") h.helicsFederateRequestNextStep(fed) h.helicsFederateLogInfoMessage(fed, "test MEXAGE") h.helicsFederateRequestNextStep(fed) h.helicsFederateLogInfoMessage(fed, "test MEXAGE") h.helicsFederateRequestNextStep(fed) h.helicsFederateFinalize(fed) assert userdata.x == 9 h.helicsFederateFree(fed) h.helicsFederateInfoFree(fi) h.helicsBrokerDisconnect(broker) h.helicsBrokerFree(broker) h.helicsCleanupLibrary() h.helicsCloseLibrary()
def test_bad_input_type_publication_2_tests(): broker = createBroker(1) vFed1, fedinfo = createValueFederate(1, "test") pubid = h.helicsFederateRegisterGlobalTypePublication( vFed1, "pub1", "string", "") with pt.raises(h.HelicsException): # @test_throws h.HELICSErrorRegistrationFailure pubid2 = h.helicsFederateRegisterGlobalTypePublication( vFed1, "pub1", "string", "") with pt.raises(h.HelicsException): # @test_throws h.HELICSErrorInvalidArgument h.helicsFederateRegisterFromPublicationJSON(vFed1, "unknownfile.json") with pt.raises(h.HelicsException): # @test_throws h.HELICSErrorExternalArgument h.helicsFederateRegisterInterfaces(vFed1, "unknownfile.json") subid = h.helicsFederateRegisterTypeInput(vFed1, "inp1", "string", "") # @test_throws h.HELICSErrorRegistrationFailure with pt.raises(h.HelicsException): subid2 = h.helicsFederateRegisterTypeInput(vFed1, "inp1", "string", "") h.helicsInputAddTarget(subid, "pub1") h.helicsFederateSetTimeProperty(vFed1, h.HELICS_PROPERTY_TIME_PERIOD, 1.0) h.helicsFederateEnterExecutingModeIterativeAsync( vFed1, h.HELICS_ITERATION_REQUEST_NO_ITERATION) res = h.helicsFederateEnterExecutingModeIterativeComplete(vFed1) assert res == h.HELICS_ITERATION_RESULT_NEXT_STEP h.helicsPublicationPublishTime(pubid, 27.0) # @test_throws h.HELICSErrorInvalidArgument with pt.raises(h.HelicsException): h.helicsFederatePublishJSON(vFed1, "unknownfile.json") h.helicsFederateRequestNextStep(vFed1) string = h.helicsInputGetString(subid) assert string[0] == "2" assert string[1] == "7" h.helicsFederateClearUpdates(vFed1) h.helicsFederateFinalize(vFed1) # @test_throws h.HELICSErrorInvalidFunctionCall with pt.raises(h.HelicsException): h.helicsPublicationPublishRaw(pubid, string.encode()) # @test_throws h.HELICSErrorInvalidFunctionCall with pt.raises(h.HelicsException): h.helicsPublicationPublishString(pubid, string) # @test_throws h.HELICSErrorInvalidFunctionCall with pt.raises(h.HelicsException): h.helicsPublicationPublishInteger(pubid, 5) # @test_throws h.HELICSErrorInvalidFunctionCall with pt.raises(h.HelicsException): h.helicsPublicationPublishBoolean(pubid, True) # @test_throws h.HELICSErrorInvalidFunctionCall with pt.raises(h.HelicsException): h.helicsPublicationPublishDouble(pubid, 39.2) # @test_throws h.HELICSErrorInvalidFunctionCall with pt.raises(h.HelicsException): h.helicsPublicationPublishTime(pubid, 19.2) # @test_throws h.HELICSErrorInvalidFunctionCall with pt.raises(h.HelicsException): h.helicsPublicationPublishChar(pubid, "a") # @test_throws h.HELICSErrorInvalidFunctionCall with pt.raises(h.HelicsException): h.helicsPublicationPublishComplex(pubid, 2.5 + -9.8j) # @test_throws h.HELICSErrorInvalidFunctionCall with pt.raises(h.HelicsException): h.helicsPublicationPublishVector(pubid, [1.3, 2.9]) # @test_throws h.HELICSErrorInvalidFunctionCall with pt.raises(h.HelicsException): h.helicsPublicationPublishNamedPoint(pubid, "hello world", 2.0) destroyFederate(vFed1, fedinfo) destroyBroker(broker)
def test_valuefederate_default_value_tests(): broker = createBroker() vFed1, fedinfo = createValueFederate(1, "fed0") inp_raw1 = h.helicsFederateRegisterInput(vFed1, "key1", h.HELICS_DATA_TYPE_RAW, "raw") inp_raw2 = h.helicsFederateRegisterInput(vFed1, "key2", h.HELICS_DATA_TYPE_RAW, "raw") inp_bool = h.helicsFederateRegisterInput(vFed1, "key3", h.HELICS_DATA_TYPE_BOOLEAN, "") inp_time = h.helicsFederateRegisterInput(vFed1, "key4", h.HELICS_DATA_TYPE_TIME, "") inp_char = h.helicsFederateRegisterInput(vFed1, "key5", h.HELICS_DATA_TYPE_STRING, "") inp_vect = h.helicsFederateRegisterInput(vFed1, "key6", h.HELICS_DATA_TYPE_VECTOR, "V") inp_double = h.helicsFederateRegisterInput(vFed1, "key7", h.HELICS_DATA_TYPE_DOUBLE, "kW") inp_double2 = h.helicsFederateRegisterInput(vFed1, "key8", h.HELICS_DATA_TYPE_DOUBLE, "") inp_np = h.helicsFederateRegisterInput(vFed1, "key9", h.HELICS_DATA_TYPE_NAMED_POINT, "") h.helicsInputSetMinimumChange(inp_double, 1100.0) h.helicsInputSetDefaultDouble(inp_double, 10000.0) h.helicsInputSetOption(inp_double2, h.HELICS_HANDLE_OPTION_CONNECTION_REQUIRED, True) pub = h.helicsFederateRegisterPublication(vFed1, "", h.HELICS_DATA_TYPE_INT, "MW") h.helicsPublicationSetOption(pub, h.HELICS_HANDLE_OPTION_CONNECTION_REQUIRED, True) h.helicsPublicationAddTarget(pub, "Testfed0/key7") h.helicsPublicationAddTarget(pub, "Testfed0/key8") h.helicsInputSetDefaultRaw(inp_raw1, "") data = "this is a string" h.helicsInputSetDefaultRaw(inp_raw2, data) h.helicsInputSetDefaultBoolean(inp_bool, True) h.helicsInputSetDefaultTime(inp_time, 12.3) h.helicsInputSetDefaultChar(inp_char, "q") h.helicsInputSetDefaultVector(inp_vect, []) h.helicsInputSetDefaultNamedPoint(inp_np, data, 15.7) h.helicsFederateEnterExecutingMode(vFed1) assert h.helicsInputGetInjectionUnits(inp_double) == "MW" assert h.helicsInputGetInjectionUnits(inp_double2) == "MW" assert h.helicsInputGetType(inp_double) == "double" assert h.helicsInputGetPublicationType(inp_double) == "int64" c2 = h.helicsInputGetChar(inp_char) assert c2 == "q" h.helicsInputGetVector(inp_vect) optset = h.helicsInputGetOption(inp_double2, h.HELICS_HANDLE_OPTION_CONNECTION_REQUIRED) assert optset == 1 optset = h.helicsPublicationGetOption(pub, h.HELICS_HANDLE_OPTION_CONNECTION_REQUIRED) assert optset == 1 h.helicsPublicationPublishInteger(pub, 12) h.helicsFederateRequestNextStep(vFed1) assert h.helicsInputGetDouble(inp_double) == 12000.0 assert h.helicsInputGetDouble(inp_double2) == 12.0 h.helicsPublicationPublishInteger(pub, 13) h.helicsFederateRequestNextStep(vFed1) assert h.helicsInputIsUpdated(inp_double) is False assert h.helicsInputIsUpdated(inp_double2) is True assert h.helicsInputGetDouble(inp_double) == 12000.0 assert h.helicsInputGetDouble(inp_double2) == 13.0 h.helicsPublicationPublishInteger(pub, 15) h.helicsFederateRequestNextStep(vFed1) assert h.helicsInputIsUpdated(inp_double) is True assert h.helicsInputIsUpdated(inp_double2) is True h.helicsInputClearUpdate(inp_double) h.helicsInputClearUpdate(inp_double2) assert h.helicsInputIsUpdated(inp_double) is False assert h.helicsInputIsUpdated(inp_double2) is False _, rval = h.helicsInputGetNamedPoint(inp_np) assert rval == 15.7 out, rval = h.helicsInputGetNamedPoint(inp_np) assert out == "this is a string" assert rval == 15.7 h.helicsFederateFinalize(vFed1) destroyFederate(vFed1, fedinfo) destroyBroker(broker)