summaryrefslogtreecommitdiff
path: root/tests/core/cancellable.c
blob: 38848bb2b6905907b13bc202aeee87a56de21578 (plain)
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
77
78
79
80
81
82
83
84
85
86
#include "clar_libgit2.h"
#include "array.h"

void test_core_cancellable__can_cancel(void)
{
	git_cancellable_source *cs;
	git_cancellable *token;

	cl_git_pass(git_cancellable_source_new(&cs));
	token = git_cancellable_source_token(cs);

	cl_assert(!git_cancellable_is_cancelled(token));
	git_cancellable_source_cancel(cs);
	cl_assert(git_cancellable_is_cancelled(token));

	git_cancellable_source_free(cs);
}

static void cancel_second(git_cancellable *cancellable, void *payload)
{
	git_cancellable_source *cs;

	GIT_UNUSED(cancellable);

	cs = (git_cancellable_source *) payload;
	git_cancellable_source_cancel(cs);
}

void test_core_cancellable__can_register(void)
{
	git_cancellable_source *cs1, *cs2;
	git_cancellable *token1, *token2;

	cl_git_pass(git_cancellable_source_new(&cs1));
	token1 = git_cancellable_source_token(cs1);

	cl_git_pass(git_cancellable_source_new(&cs2));
	token2 = git_cancellable_source_token(cs2);

	cl_git_pass(git_cancellable_register(token1, cancel_second, cs2));

	cl_assert(!git_cancellable_is_cancelled(token1));
	cl_assert(!git_cancellable_is_cancelled(token2));

	git_cancellable_source_cancel(cs1);

	cl_assert(git_cancellable_is_cancelled(token1));
	cl_assert(git_cancellable_is_cancelled(token2));

	git_cancellable_source_free(cs1);
	git_cancellable_source_free(cs2);
}

static void cancel_count(git_cancellable *cancellable, void *payload)
{
	int *i;
	GIT_UNUSED(cancellable);

	i = (int *) payload;
	*i += 1;
}

void test_core_cancellable__registration_fires_once(void)
{
	git_cancellable_source *cs;
	git_cancellable *token;
	int cancelled_times = 0;

	cl_git_pass(git_cancellable_source_new(&cs));

	token = git_cancellable_source_token(cs);
	cl_git_pass(git_cancellable_register(token, cancel_count, &cancelled_times));

	cl_assert(!git_cancellable_is_cancelled(token));

	git_cancellable_source_cancel(cs);
	cl_assert(git_cancellable_is_cancelled(token));

	git_cancellable_source_cancel(cs);
	cl_assert(git_cancellable_is_cancelled(token));

	cl_assert_equal_i(1, cancelled_times);

	git_cancellable_source_free(cs);
}