File: test_client_thread.c

package info (click to toggle)
libnetconf2 3.7.10-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,200 kB
  • sloc: ansic: 33,538; sh: 49; makefile: 21
file content (76 lines) | stat: -rw-r--r-- 1,879 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
 * \file test_client_thread.c
 * \author Radek Krejci <rkrejci@cesnet.cz>
 * \brief libnetconf2 tests - threads functions in client
 *
 * Copyright (c) 2017 CESNET, z.s.p.o.
 *
 * This source code is licensed under BSD 3-Clause License (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/BSD-3-Clause
 */

#define _GNU_SOURCE

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include <libyang/libyang.h>

#include "ln2_test.h"

#define nc_assert(cond) if (!(cond)) { fprintf(stderr, "assert failed (%s:%d)\n", __FILE__, __LINE__); exit(1); }

static void *
thread(void *arg)
{
    /* default search path is NULL */
    nc_assert(nc_client_get_schema_searchpath() == NULL);

    /* use the context shared from the main thread */
    nc_client_set_thread_context(arg);

    /* check that we have now the search path set in main thread */
    nc_assert(strcmp(nc_client_get_schema_searchpath(), "/tmp") == 0);
    /* and change it to check it later in main thread */
    nc_assert(nc_client_set_schema_searchpath("/etc") == 0)

    return NULL;
}

int
main(void)
{
    void *arg;
    pthread_t t;
    int r;

    /*
     * TEST sharing the thread context
     */
    nc_assert(nc_client_set_schema_searchpath("/tmp") == 0)

    /* get the context for sharing */
    arg = nc_client_get_thread_context();

    /* create new thread and provide the context */
    r = pthread_create(&t, NULL, &thread, arg);
    nc_assert(r == 0);

    pthread_join(t, NULL);

    /* check the changed search path value from the thread */
    nc_assert(strcmp(nc_client_get_schema_searchpath(), "/etc") == 0);

    /* cleanup */
    nc_client_destroy();

    return EXIT_SUCCESS;
}