blob: 7df78193ac24aec07155c1de024b54e553dad408 [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
15#include <pthread.h>
Radek Krejci2e32e392017-05-26 13:25:46 +020016#include <stdio.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020017#include <stdlib.h>
Radek Krejci2e32e392017-05-26 13:25:46 +020018#include <string.h>
19#include <sys/types.h>
20#include <sys/wait.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020021#include <unistd.h>
Radek Krejci2e32e392017-05-26 13:25:46 +020022
23#include <libyang/libyang.h>
24
25#include <session_client.h>
Jan Kundrátcf15d6c2017-10-26 18:07:56 +020026#include "tests/config.h"
Radek Krejci2e32e392017-05-26 13:25:46 +020027
28#define nc_assert(cond) if (!(cond)) { fprintf(stderr, "assert failed (%s:%d)\n", __FILE__, __LINE__); exit(1); }
29
30static void *
31thread(void *arg)
32{
33 /* default search path is NULL */
34 nc_assert(nc_client_get_schema_searchpath() == NULL);
35
36 /* use the context shared from the main thread */
37 nc_client_set_thread_context(arg);
38
39 /* check that we have now the search path set in main thread */
40 nc_assert(strcmp(nc_client_get_schema_searchpath(), "/tmp") == 0);
41 /* and change it to check it later in main thread */
42 nc_assert(nc_client_set_schema_searchpath("/etc") == 0)
43
44 return NULL;
45}
46
47int
48main(void)
49{
50 void *arg;
51 pthread_t t;
52 int r;
53
54 nc_client_init();
55
56 /*
57 * TEST sharing the thread context
58 */
59 nc_assert(nc_client_set_schema_searchpath("/tmp") == 0)
60
61 /* get the context for sharing */
62 arg = nc_client_get_thread_context();
63
64 /* create new thread and provide the context */
65 r = pthread_create(&t, NULL, &thread, arg);
66 nc_assert(r == 0);
67
68 pthread_join(t, NULL);
69
70 /* check the changed search path value from the thread */
71 nc_assert(strcmp(nc_client_get_schema_searchpath(), "/etc") == 0);
72
73 /* cleanup */
74 nc_client_destroy();
75
76 return EXIT_SUCCESS;
77}