Skip to content

Commit d510d05

Browse files
committed
[TEST] Add test case for lxm service internal API
- Added positive and negative TCs for lxm service internal API Signed-off-by: hyunil park <hyunil46.park@samsung.com>
1 parent ea06b44 commit d510d05

File tree

4 files changed

+359
-78
lines changed

4 files changed

+359
-78
lines changed

c/include/ml-lxm-service-internal.h

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,24 @@
3434
*
3535
* Basic usage workflow:
3636
* @code
37-
* // 1. Create session
37+
* // 1. Create session with callback
3838
* ml_lxm_session_h session;
39-
* ml_lxm_session_create("/path/to/config.json", NULL, &session);
39+
* ml_lxm_session_create("/path/to/config.json", NULL, token_handler, NULL, &session);
4040
*
4141
* // 2. Create prompt
4242
* ml_lxm_prompt_h prompt;
4343
* ml_lxm_prompt_create(&prompt);
4444
* ml_lxm_prompt_append_text(prompt, "Hello AI");
4545
*
46-
* // 3. Set callback using ml_lxm_session_set_event_cb
47-
* ml_lxm_session_set_event_cb(session, token_handler, NULL);
48-
*
49-
* // 4. Generate response with options
46+
* // 3. Generate response with options (callback is already set during session creation)
5047
* ml_option_h options = NULL;
5148
* ml_option_create(&options);
5249
* ml_option_set(options, "temperature", g_strdup_printf("%f", 1.0), g_free);
5350
* ml_option_set(options, "max_tokens", g_strdup_printf("%zu", (size_t)50), g_free);
5451
* ml_lxm_session_respond(session, prompt, options);
5552
* ml_option_destroy(options);
5653
*
57-
* // 5. Cleanup
54+
* // 4. Cleanup
5855
* ml_lxm_prompt_destroy(prompt);
5956
* ml_lxm_session_destroy(session);
6057
* @endcode
@@ -81,8 +78,8 @@
8178
* return -1;
8279
* }
8380
*
84-
* // 1. Create session with config and instructions
85-
* ret = ml_lxm_session_create("/path/to/config.json", "You are a helpful AI assistant", &session);
81+
* // 1. Create session with config, instructions, and callback
82+
* ret = ml_lxm_session_create("/path/to/config.json", "You are a helpful AI assistant", token_handler, NULL, &session);
8683
* if (ret != ML_ERROR_NONE) {
8784
* std::cout << "Failed to create session" << std::endl;
8885
* return -1;
@@ -105,16 +102,7 @@
105102
* return -1;
106103
* }
107104
*
108-
* // 3. Set callback using ml_lxm_session_set_event_cb
109-
* ret = ml_lxm_session_set_event_cb(session, token_handler, NULL);
110-
* if (ret != ML_ERROR_NONE) {
111-
* std::cout << "Failed to set callback" << std::endl;
112-
* ml_lxm_prompt_destroy(prompt);
113-
* ml_lxm_session_destroy(session);
114-
* return -1;
115-
* }
116-
*
117-
* // 4. Generate response with custom options
105+
* // 3. Generate response with custom options
118106
* ml_option_h options = NULL;
119107
* ml_option_create(&options);
120108
* ml_option_set(options, "temperature", g_strdup_printf("%f", 1.2), g_free);
@@ -128,7 +116,7 @@
128116
* }
129117
* std::cout << std::endl;
130118
*
131-
* // 5. Cleanup
119+
* // 4. Cleanup
132120
* ml_lxm_prompt_destroy(prompt);
133121
* ml_lxm_session_destroy(session);
134122
*
@@ -198,13 +186,16 @@ int ml_lxm_check_availability (ml_lxm_availability_e * status);
198186
typedef void *ml_lxm_session_h;
199187

200188
/**
201-
* @brief Creates an LXM session.
189+
* @brief Creates an LXM session with mandatory callback.
202190
* @param[in] config_path Path to configuration file.
203191
* @param[in] instructions Initial instructions (optional).
192+
* @param[in] callback Callback function for session events (mandatory).
193+
* @param[in] user_data User data to be passed to the callback.
204194
* @param[out] session Session handle.
205195
* @return ML_ERROR_NONE on success.
196+
* @note The callback parameter is mandatory and will be set during session creation.
206197
*/
207-
int ml_lxm_session_create (const char *config_path, const char *instructions, ml_lxm_session_h * session);
198+
int ml_lxm_session_create (const char *config_path, const char *instructions, ml_service_event_cb callback, void *user_data, ml_lxm_session_h * session);
208199

209200
/**
210201
* @brief Destroys an LXM session.
@@ -256,15 +247,6 @@ int ml_lxm_prompt_destroy (ml_lxm_prompt_h prompt);
256247
*/
257248
int ml_lxm_session_set_instructions (ml_lxm_session_h session, const char *instructions);
258249

259-
/**
260-
* @brief Sets the callback for LXM session events.
261-
* @param[in] session Session handle.
262-
* @param[in] callback Callback function for session events.
263-
* @param[in] user_data User data to be passed to the callback.
264-
* @return ML_ERROR_NONE on success.
265-
* @note This function should be called once per session. Calling it multiple times will return an error.
266-
*/
267-
int ml_lxm_session_set_event_cb (ml_lxm_session_h session, ml_service_event_cb callback, void *user_data);
268250

269251
/**
270252
* @brief Generates an token-streamed response.

c/src/ml-lxm-service.c

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ typedef struct
2121
ml_service_h service_handle;
2222
char *config_path;
2323
char *instructions;
24-
25-
/* Callback related fields */
2624
ml_service_event_cb user_callback; /**< User callback function */
2725
void *user_data; /**< User data passed to callback */
28-
gboolean callback_set; /**< Flag indicating if callback is set */
2926
} ml_lxm_session_internal;
3027

3128
/**
@@ -50,21 +47,24 @@ ml_lxm_check_availability (ml_lxm_availability_e * status)
5047
}
5148

5249
/**
53-
* @brief Creates an LXM session.
50+
* @brief Creates an LXM session with mandatory callback.
5451
* @param[in] config_path Path to configuration file.
5552
* @param[in] instructions Initial instructions (optional).
53+
* @param[in] callback Callback function for session events (mandatory).
54+
* @param[in] user_data User data to be passed to the callback.
5655
* @param[out] session Session handle.
5756
* @return ML_ERROR_NONE on success.
57+
* @note The callback parameter is mandatory and will be set during session creation.
5858
*/
5959
int
6060
ml_lxm_session_create (const char *config_path, const char *instructions,
61-
ml_lxm_session_h * session)
61+
ml_service_event_cb callback, void *user_data, ml_lxm_session_h * session)
6262
{
6363
ml_service_h handle;
6464
ml_lxm_session_internal *s;
6565
int ret;
6666

67-
if (!session || !config_path)
67+
if (!session || !config_path || !callback)
6868
return ML_ERROR_INVALID_PARAMETER;
6969

7070
ret = ml_service_new (config_path, &handle);
@@ -81,49 +81,24 @@ ml_lxm_session_create (const char *config_path, const char *instructions,
8181
s->config_path = g_strdup (config_path);
8282
s->instructions = g_strdup (instructions);
8383
s->service_handle = handle;
84-
85-
/* Initialize callback related fields */
86-
s->user_callback = NULL;
87-
s->user_data = NULL;
88-
s->callback_set = FALSE;
89-
90-
*session = s;
91-
92-
return ML_ERROR_NONE;
93-
}
94-
95-
/**
96-
* @brief Sets the callback for LXM session events.
97-
* @param[in] session Session handle.
98-
* @param[in] callback Callback function for session events.
99-
* @param[in] user_data User data to be passed to the callback.
100-
* @return ML_ERROR_NONE on success.
101-
* @note This function should be called once per session. Calling it multiple times will return an error.
102-
*/
103-
int
104-
ml_lxm_session_set_event_cb (ml_lxm_session_h session,
105-
ml_service_event_cb callback, void *user_data)
106-
{
107-
ml_lxm_session_internal *s;
108-
int ret;
109-
110-
if (!session || !callback)
111-
return ML_ERROR_INVALID_PARAMETER;
112-
113-
s = (ml_lxm_session_internal *) session;
114-
115-
if (s->callback_set)
116-
return ML_ERROR_INVALID_PARAMETER;
84+
s->user_callback = callback;
85+
s->user_data = user_data;
11786

11887
ret = ml_service_set_event_cb (s->service_handle, callback, user_data);
119-
if (ret != ML_ERROR_NONE)
88+
if (ret != ML_ERROR_NONE) {
89+
/* Cleanup on failure */
90+
ml_service_destroy (s->service_handle);
91+
g_free (s->config_path);
92+
g_free (s->instructions);
93+
g_free (s);
12094
return ret;
95+
}
12196

122-
s->callback_set = TRUE;
123-
97+
*session = s;
12498
return ML_ERROR_NONE;
12599
}
126100

101+
127102
/**
128103
* @brief Destroys an LXM session.
129104
* @param[in] session Session handle.
@@ -252,7 +227,7 @@ ml_lxm_session_set_instructions (ml_lxm_session_h session,
252227
* @param[in] prompt Prompt handle.
253228
* @param[in] options Generation parameters (ml_option_h).
254229
* @return ML_ERROR_NONE on success.
255-
* @note The callback should be set using ml_service_set_event_cb() before calling this function.
230+
* @note The callback is automatically set during session creation.
256231
*/
257232
int
258233
ml_lxm_session_respond (ml_lxm_session_h session,
@@ -271,10 +246,6 @@ ml_lxm_session_respond (ml_lxm_session_h session,
271246
s = (ml_lxm_session_internal *) session;
272247
p = (ml_lxm_prompt_internal *) prompt;
273248

274-
/* Check if callback is set */
275-
if (!s->callback_set)
276-
return ML_ERROR_STREAMS_PIPE;
277-
278249
full_input = g_string_new ("");
279250
if (s->instructions && s->instructions[0] != '\0') {
280251
g_string_append (full_input, s->instructions);

tests/capi/meson.build

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ if get_option('enable-ml-service')
7676
test('unittest_capi_service_training_offloading', unittest_capi_service_training_offloading, env: testenv, timeout: 100)
7777
endif
7878
endif
79+
80+
# LXM Service Tests
81+
# These tests require both ml-service and llamacpp to be enabled.
82+
llamacpp_dep = dependency('llama', required: false)
83+
if llamacpp_dep.found()
84+
# Note: The source file itself is also conditionally compiled with ENABLE_LLAMACPP.
85+
unittest_capi_lxm_service = executable('unittest_capi_lxm_service',
86+
'unittest_capi_lxm_service.cc',
87+
dependencies: service_unittest_deps,
88+
install: get_option('install-test'),
89+
install_dir: unittest_install_dir
90+
)
91+
test('unittest_capi_lxm_service', unittest_capi_lxm_service, env: testenv, timeout: 120) # Increased timeout for LLM response
92+
else
93+
message('LXM Service tests will be skipped because llama dependency was not found.')
94+
endif
7995
endif
8096

8197
if nnfw_dep.found()

0 commit comments

Comments
 (0)