Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Grpc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tci-gateway-module
Grpc
Commits
93360e1f
Commit
93360e1f
authored
9 years ago
by
Sree Kuchibhotla
Browse files
Options
Downloads
Patches
Plain Diff
Add test coverage for gpr_refn() function
parent
045c8483
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
test/core/support/sync_test.c
+34
-21
34 additions, 21 deletions
test/core/support/sync_test.c
with
34 additions
and
21 deletions
test/core/support/sync_test.c
+
34
−
21
View file @
93360e1f
...
...
@@ -153,6 +153,7 @@ struct test {
gpr_int64
counter
;
int
thread_count
;
/* used to allocate thread ids */
int
done
;
/* threads not yet completed */
int
incr_step
;
/* how much to increment/decrement refcount each time */
gpr_mu
mu
;
/* protects iterations, counter, thread_count, done */
...
...
@@ -170,13 +171,14 @@ struct test {
};
/* Return pointer to a new struct test. */
static
struct
test
*
test_new
(
int
threads
,
gpr_int64
iterations
)
{
static
struct
test
*
test_new
(
int
threads
,
gpr_int64
iterations
,
int
incr_step
)
{
struct
test
*
m
=
gpr_malloc
(
sizeof
(
*
m
));
m
->
threads
=
threads
;
m
->
iterations
=
iterations
;
m
->
counter
=
0
;
m
->
thread_count
=
0
;
m
->
done
=
threads
;
m
->
incr_step
=
incr_step
;
gpr_mu_init
(
&
m
->
mu
);
gpr_cv_init
(
&
m
->
cv
);
gpr_cv_init
(
&
m
->
done_cv
);
...
...
@@ -238,9 +240,12 @@ static void mark_thread_done(struct test *m) {
/* Test several threads running (*body)(struct test *m) for increasing settings
of m->iterations, until about timeout_s to 2*timeout_s seconds have elapsed.
If extra!=NULL, run (*extra)(m) in an additional thread. */
If extra!=NULL, run (*extra)(m) in an additional thread.
incr_step controls by how much m->refcount should be incremented/decremented
(if at all) each time in the tests.
*/
static
void
test
(
const
char
*
name
,
void
(
*
body
)(
void
*
m
),
void
(
*
extra
)(
void
*
m
),
int
timeout_s
)
{
void
(
*
extra
)(
void
*
m
),
int
timeout_s
,
int
incr_step
)
{
gpr_int64
iterations
=
1024
;
struct
test
*
m
;
gpr_timespec
start
=
gpr_now
(
GPR_CLOCK_REALTIME
);
...
...
@@ -251,7 +256,7 @@ static void test(const char *name, void (*body)(void *m),
while
(
gpr_time_cmp
(
gpr_now
(
GPR_CLOCK_REALTIME
),
deadline
)
<
0
)
{
iterations
<<=
1
;
fprintf
(
stderr
,
" %ld"
,
(
long
)
iterations
);
m
=
test_new
(
10
,
iterations
);
m
=
test_new
(
10
,
iterations
,
incr_step
);
if
(
extra
!=
NULL
)
{
gpr_thd_id
id
;
GPR_ASSERT
(
gpr_thd_new
(
&
id
,
extra
,
m
,
NULL
));
...
...
@@ -259,7 +264,7 @@ static void test(const char *name, void (*body)(void *m),
}
test_create_threads
(
m
,
body
);
test_wait
(
m
);
if
(
m
->
counter
!=
m
->
threads
*
m
->
iterations
)
{
if
(
m
->
counter
!=
m
->
threads
*
m
->
iterations
*
m
->
incr_step
)
{
fprintf
(
stderr
,
"counter %ld threads %d iterations %ld
\n
"
,
(
long
)
m
->
counter
,
m
->
threads
,
(
long
)
m
->
iterations
);
GPR_ASSERT
(
0
);
...
...
@@ -406,14 +411,18 @@ static void statsinc(void *v /*=m*/) {
mark_thread_done
(
m
);
}
/* Increment m->refcount m->iterations times
, d
ecrement
m->thread_refcount
once, and if it reaches zero, set m->event to (void*)1;
then mark thread as
done. */
/* Increment m->refcount
by m->incr_step for
m->iterations times
. D
ecrement
m->thread_refcount
once, and if it reaches zero, set m->event to (void*)1;
then mark thread as
done. */
static
void
refinc
(
void
*
v
/*=m*/
)
{
struct
test
*
m
=
v
;
gpr_int64
i
;
for
(
i
=
0
;
i
!=
m
->
iterations
;
i
++
)
{
gpr_ref
(
&
m
->
refcount
);
if
(
m
->
incr_step
==
1
)
{
gpr_ref
(
&
m
->
refcount
);
}
else
{
gpr_refn
(
&
m
->
refcount
,
m
->
incr_step
);
}
}
if
(
gpr_unref
(
&
m
->
thread_refcount
))
{
gpr_event_set
(
&
m
->
event
,
(
void
*
)
1
);
...
...
@@ -421,12 +430,13 @@ static void refinc(void *v /*=m*/) {
mark_thread_done
(
m
);
}
/* Wait until m->event is set to (void *)1, then decrement m->refcount
m->stats_counter m->iterations times, and ensure that the last decrement
caused the counter to reach zero, then mark thread as done. */
/* Wait until m->event is set to (void *)1, then decrement m->refcount by 1
(m->threads * m->iterations * m->incr_step) times, and ensure that the last
decrement caused the counter to reach zero, then mark thread as done. */
static
void
refcheck
(
void
*
v
/*=m*/
)
{
struct
test
*
m
=
v
;
gpr_int64
n
=
m
->
iterations
*
m
->
threads
;
gpr_int64
n
=
m
->
iterations
*
m
->
threads
*
m
->
incr_step
;
gpr_int64
i
;
GPR_ASSERT
(
gpr_event_wait
(
&
m
->
event
,
gpr_inf_future
(
GPR_CLOCK_REALTIME
))
==
(
void
*
)
1
);
...
...
@@ -444,13 +454,16 @@ static void refcheck(void *v /*=m*/) {
int
main
(
int
argc
,
char
*
argv
[])
{
grpc_test_init
(
argc
,
argv
);
test
(
"mutex"
,
&
inc
,
NULL
,
1
);
test
(
"mutex try"
,
&
inctry
,
NULL
,
1
);
test
(
"cv"
,
&
inc_by_turns
,
NULL
,
1
);
test
(
"timedcv"
,
&
inc_with_1ms_delay
,
NULL
,
1
);
test
(
"queue"
,
&
many_producers
,
&
consumer
,
10
);
test
(
"stats_counter"
,
&
statsinc
,
NULL
,
1
);
test
(
"refcount"
,
&
refinc
,
&
refcheck
,
1
);
test
(
"timedevent"
,
&
inc_with_1ms_delay_event
,
NULL
,
1
);
test
(
"mutex"
,
&
inc
,
NULL
,
1
,
1
);
test
(
"mutex try"
,
&
inctry
,
NULL
,
1
,
1
);
test
(
"cv"
,
&
inc_by_turns
,
NULL
,
1
,
1
);
test
(
"timedcv"
,
&
inc_with_1ms_delay
,
NULL
,
1
,
1
);
test
(
"queue"
,
&
many_producers
,
&
consumer
,
10
,
1
);
test
(
"stats_counter"
,
&
statsinc
,
NULL
,
1
,
1
);
test
(
"refcount by 1"
,
&
refinc
,
&
refcheck
,
1
,
1
);
test
(
"refcount by 3"
,
&
refinc
,
&
refcheck
,
1
,
3
);
/* incr_step of 3 is an
arbitrary choice. Any
number > 1 is okay here */
test
(
"timedevent"
,
&
inc_with_1ms_delay_event
,
NULL
,
1
,
1
);
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment