blob: 98537852fcc619935881759f3edc8c8b2a51522d [file] [log] [blame]
Radek Krejci2e32e392017-05-26 13:25:46 +02001/**
2 * \file test_client_thread.c
3 * \author Radek Krejci <rkrejci@cesnet.cz>
4 * \brief libnetconf2 tests - threads functions in client
5 *
6 * Copyright (c) 2017 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Michal Vaskoba9f3582023-02-22 10:26:32 +010015#define _GNU_SOURCE
16
Radek Krejci2e32e392017-05-26 13:25:46 +020017#include <pthread.h>
Radek Krejci2e32e392017-05-26 13:25:46 +020018#include <stdio.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020019#include <stdlib.h>
Radek Krejci2e32e392017-05-26 13:25:46 +020020#include <string.h>
21#include <sys/types.h>
22#include <sys/wait.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020023#include <unistd.h>
Radek Krejci2e32e392017-05-26 13:25:46 +020024
25#include <libyang/libyang.h>
26
roman2ca058f2024-08-14 10:35:36 +020027#include "ln2_test.h"
Radek Krejci2e32e392017-05-26 13:25:46 +020028
29#define nc_assert(cond) if (!(cond)) { fprintf(stderr, "assert failed (%s:%d)\n", __FILE__, __LINE__); exit(1); }
30
31static void *
32thread(void *arg)
33{
34 /* default search path is NULL */
35 nc_assert(nc_client_get_schema_searchpath() == NULL);
36
37 /* use the context shared from the main thread */
38 nc_client_set_thread_context(arg);
39
40 /* check that we have now the search path set in main thread */
41 nc_assert(strcmp(nc_client_get_schema_searchpath(), "/tmp") == 0);
42 /* and change it to check it later in main thread */
43 nc_assert(nc_client_set_schema_searchpath("/etc") == 0)
44
45 return NULL;
46}
47
48int
49main(void)
50{
51 void *arg;
52 pthread_t t;
53 int r;
54
Radek Krejci2e32e392017-05-26 13:25:46 +020055 /*
56 * TEST sharing the thread context
57 */
58 nc_assert(nc_client_set_schema_searchpath("/tmp") == 0)
59
60 /* get the context for sharing */
61 arg = nc_client_get_thread_context();
62
63 /* create new thread and provide the context */
64 r = pthread_create(&t, NULL, &thread, arg);
65 nc_assert(r == 0);
66
67 pthread_join(t, NULL);
68
69 /* check the changed search path value from the thread */
70 nc_assert(strcmp(nc_client_get_schema_searchpath(), "/etc") == 0);
71
72 /* cleanup */
73 nc_client_destroy();
74
75 return EXIT_SUCCESS;
76}