feat: add mock JACK test target and unit tests for carla host

This commit is contained in:
Loic Coenen
2026-05-17 10:18:58 +00:00
parent e6e0a47749
commit d28e1f45f5
7 changed files with 247 additions and 105 deletions

View File

@@ -1,13 +1,38 @@
#include <CarlaHost.h>
#include <CarlaBackend.h>
#include <jack/jack.h>
#include <string.h>
#include "carla_host.h"
#ifdef MOCK_JACK
/* Mock JACK functions always succeed */
/* Provide a dummy type so we can have a nonNULL pointer */
typedef void jack_client_t;
static int mock_jack_connect(const char *from, const char *to) {
(void)from; (void)to;
return 0;
}
static int mock_jack_disconnect(const char *from, const char *to) {
(void)from; (void)to;
return 0;
}
/* Provide a fake jack_client pointer that is nonNULL */
#define jack_client ((jack_client_t*)1)
/* Real jack_connect/jack_disconnect take 3 arguments (client, a, b).
We ignore the client and forward to the mock 2arg functions. */
#define jack_connect(client, a, b) ((void)(client), mock_jack_connect(a, b))
#define jack_disconnect(client, a, b) ((void)(client), mock_jack_disconnect(a, b))
#else
#include <jack/jack.h>
#endif
#define MAX_PLUGINS 256
static CarlaHostHandle handle = NULL;
#ifdef MOCK_JACK
/* jack_client is defined via macro above (nonNULL) */
#else
static jack_client_t *jack_client = NULL; // private JACK client for port connections
#endif
static int carla_pids[MAX_PLUGINS];
static int plugin_count = 0;
@@ -25,16 +50,20 @@ static int conn_count = 0;
int carla_init_jack(void) {
if (handle != NULL) return 0;
#ifndef MOCK_JACK
// 1) Open our own JACK client (for port connections)
jack_status_t status;
jack_client = jack_client_open("looper-connector", JackNoStartServer, &status);
// It's okay if jack_client is NULL; we still try Carla
#endif
// 2) Create the Carla host handle
handle = carla_standalone_host_init();
if (!handle) {
#ifndef MOCK_JACK
if (jack_client) jack_client_close(jack_client);
jack_client = NULL;
#endif
return -1;
}
@@ -42,8 +71,10 @@ int carla_init_jack(void) {
if (!carla_engine_init(handle, "JACK", "looper-client")) {
carla_engine_close(handle);
handle = NULL;
#ifndef MOCK_JACK
if (jack_client) jack_client_close(jack_client);
jack_client = NULL;
#endif
return -1;
}
return 0;
@@ -54,10 +85,12 @@ void carla_cleanup_jack(void) {
carla_engine_close(handle);
handle = NULL;
}
#ifndef MOCK_JACK
if (jack_client) {
jack_client_close(jack_client);
jack_client = NULL;
}
#endif
plugin_count = 0;
}