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