1 #include "clar_libgit2.h"
2 #include "repository.h"
4 static git_repository
*g_repo
;
6 void test_object_cache__initialize(void)
11 void test_object_cache__cleanup(void)
13 git_repository_free(g_repo
);
16 git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT
, (int)GIT_OBJECT_BLOB
, (size_t)0);
24 { GIT_OBJECT_BLOB
, "a8233120f6ad708f843d861ce2b7228ec4e3dec6" }, /* README */
25 { GIT_OBJECT_BLOB
, "3697d64be941a53d4ae8f6a271e4e3fa56b022cc" }, /* branch_file.txt */
26 { GIT_OBJECT_BLOB
, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd" }, /* new.txt */
28 /* refs/heads/subtrees */
29 { GIT_OBJECT_BLOB
, "1385f264afb75a56a5bec74243be9b367ba4ca08" }, /* README */
30 { GIT_OBJECT_TREE
, "f1425cef211cc08caa31e7b545ffb232acb098c3" }, /* ab */
31 { GIT_OBJECT_BLOB
, "d6c93164c249c8000205dd4ec5cbca1b516d487f" }, /* ab/4.txt */
32 { GIT_OBJECT_TREE
, "9a03079b8a8ee85a0bee58bf9be3da8b62414ed4" }, /* ab/c */
33 { GIT_OBJECT_BLOB
, "270b8ea76056d5cad83af921837702d3e3c2924d" }, /* ab/c/3.txt */
34 { GIT_OBJECT_TREE
, "b6361fc6a97178d8fc8639fdeed71c775ab52593" }, /* ab/de */
35 { GIT_OBJECT_BLOB
, "e7b4ad382349ff96dd8199000580b9b1e2042eb0" }, /* ab/de/2.txt */
36 { GIT_OBJECT_TREE
, "3259a6bd5b57fb9c1281bb7ed3167b50f224cb54" }, /* ab/de/fgh */
37 { GIT_OBJECT_BLOB
, "1f67fc4386b2d171e0d21be1c447e12660561f9b" }, /* ab/de/fgh/1.txt */
38 { GIT_OBJECT_BLOB
, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057" }, /* branch_file.txt */
39 { GIT_OBJECT_BLOB
, "fa49b077972391ad58037050f2a75f74e3671e92" }, /* new.txt */
41 /* refs/heads/chomped */
42 { GIT_OBJECT_BLOB
, "0266163a49e280c4f5ed1e08facd36a2bd716bcf" }, /* readme.txt */
48 void test_object_cache__cache_everything(void)
52 git_odb_object
*odb_obj
;
57 GIT_OPT_SET_CACHE_OBJECT_LIMIT
, (int)GIT_OBJECT_BLOB
, (size_t)32767);
59 cl_git_pass(git_repository_open(&g_repo
, cl_fixture("testrepo.git")));
60 cl_git_pass(git_repository_odb(&odb
, g_repo
));
62 start
= (int)git_cache_size(&g_repo
->objects
);
64 for (i
= 0; g_data
[i
].sha
!= NULL
; ++i
) {
65 int count
= (int)git_cache_size(&g_repo
->objects
);
67 cl_git_pass(git_oid_fromstr(&oid
, g_data
[i
].sha
));
69 /* alternate between loading raw and parsed objects */
71 cl_git_pass(git_odb_read(&odb_obj
, odb
, &oid
));
72 cl_assert(g_data
[i
].type
== git_odb_object_type(odb_obj
));
73 git_odb_object_free(odb_obj
);
75 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
76 cl_assert(g_data
[i
].type
== git_object_type(obj
));
80 cl_assert_equal_i(count
+ 1, (int)git_cache_size(&g_repo
->objects
));
83 cl_assert_equal_i(i
, (int)git_cache_size(&g_repo
->objects
) - start
);
87 for (i
= 0; g_data
[i
].sha
!= NULL
; ++i
) {
88 int count
= (int)git_cache_size(&g_repo
->objects
);
90 cl_git_pass(git_oid_fromstr(&oid
, g_data
[i
].sha
));
91 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
92 cl_assert(g_data
[i
].type
== git_object_type(obj
));
95 cl_assert_equal_i(count
, (int)git_cache_size(&g_repo
->objects
));
99 void test_object_cache__cache_no_blobs(void)
101 int i
, start
, nonblobs
= 0;
103 git_odb_object
*odb_obj
;
107 git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT
, (int)GIT_OBJECT_BLOB
, (size_t)0);
109 cl_git_pass(git_repository_open(&g_repo
, cl_fixture("testrepo.git")));
110 cl_git_pass(git_repository_odb(&odb
, g_repo
));
112 start
= (int)git_cache_size(&g_repo
->objects
);
114 for (i
= 0; g_data
[i
].sha
!= NULL
; ++i
) {
115 int count
= (int)git_cache_size(&g_repo
->objects
);
117 cl_git_pass(git_oid_fromstr(&oid
, g_data
[i
].sha
));
119 /* alternate between loading raw and parsed objects */
121 cl_git_pass(git_odb_read(&odb_obj
, odb
, &oid
));
122 cl_assert(g_data
[i
].type
== git_odb_object_type(odb_obj
));
123 git_odb_object_free(odb_obj
);
125 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
126 cl_assert(g_data
[i
].type
== git_object_type(obj
));
127 git_object_free(obj
);
130 if (g_data
[i
].type
== GIT_OBJECT_BLOB
)
131 cl_assert_equal_i(count
, (int)git_cache_size(&g_repo
->objects
));
133 cl_assert_equal_i(count
+ 1, (int)git_cache_size(&g_repo
->objects
));
138 cl_assert_equal_i(nonblobs
, (int)git_cache_size(&g_repo
->objects
) - start
);
143 static void *cache_parsed(void *arg
)
149 for (i
= ((int *)arg
)[1]; g_data
[i
].sha
!= NULL
; i
+= 2) {
150 cl_git_pass(git_oid_fromstr(&oid
, g_data
[i
].sha
));
151 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
152 cl_assert(g_data
[i
].type
== git_object_type(obj
));
153 git_object_free(obj
);
156 for (i
= 0; i
< ((int *)arg
)[1]; i
+= 2) {
157 cl_git_pass(git_oid_fromstr(&oid
, g_data
[i
].sha
));
158 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
159 cl_assert(g_data
[i
].type
== git_object_type(obj
));
160 git_object_free(obj
);
166 static void *cache_raw(void *arg
)
171 git_odb_object
*odb_obj
;
173 cl_git_pass(git_repository_odb(&odb
, g_repo
));
175 for (i
= ((int *)arg
)[1]; g_data
[i
].sha
!= NULL
; i
+= 2) {
176 cl_git_pass(git_oid_fromstr(&oid
, g_data
[i
].sha
));
177 cl_git_pass(git_odb_read(&odb_obj
, odb
, &oid
));
178 cl_assert(g_data
[i
].type
== git_odb_object_type(odb_obj
));
179 git_odb_object_free(odb_obj
);
182 for (i
= 0; i
< ((int *)arg
)[1]; i
+= 2) {
183 cl_git_pass(git_oid_fromstr(&oid
, g_data
[i
].sha
));
184 cl_git_pass(git_odb_read(&odb_obj
, odb
, &oid
));
185 cl_assert(g_data
[i
].type
== git_odb_object_type(odb_obj
));
186 git_odb_object_free(odb_obj
);
195 #define THREADCOUNT 50
197 void test_object_cache__threadmania(void)
204 git_thread t
[THREADCOUNT
];
207 for (max_i
= 0; g_data
[max_i
].sha
!= NULL
; ++max_i
)
210 for (try = 0; try < REPEAT
; ++try) {
212 cl_git_pass(git_repository_open(&g_repo
, cl_fixture("testrepo.git")));
214 for (th
= 0; th
< THREADCOUNT
; ++th
) {
215 data
= git__malloc(2 * sizeof(int));
217 ((int *)data
)[0] = th
;
218 ((int *)data
)[1] = th
% max_i
;
220 fn
= (th
& 1) ? cache_parsed
: cache_raw
;
223 cl_git_pass(git_thread_create(&t
[th
], fn
, data
));
225 cl_assert(fn(data
) == data
);
231 for (th
= 0; th
< THREADCOUNT
; ++th
) {
232 cl_git_pass(git_thread_join(&t
[th
], &data
));
233 cl_assert_equal_i(th
, ((int *)data
)[0]);
238 git_repository_free(g_repo
);
243 static void *cache_quick(void *arg
)
248 cl_git_pass(git_oid_fromstr(&oid
, g_data
[4].sha
));
249 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
250 cl_assert(g_data
[4].type
== git_object_type(obj
));
251 git_object_free(obj
);
256 void test_object_cache__fast_thread_rush(void)
258 int try, th
, data
[THREADCOUNT
*2];
260 git_thread t
[THREADCOUNT
*2];
263 for (try = 0; try < REPEAT
; ++try) {
264 cl_git_pass(git_repository_open(&g_repo
, cl_fixture("testrepo.git")));
266 for (th
= 0; th
< THREADCOUNT
*2; ++th
) {
270 git_thread_create(&t
[th
], cache_quick
, &data
[th
]));
272 cl_assert(cache_quick(&data
[th
]) == &data
[th
]);
277 for (th
= 0; th
< THREADCOUNT
*2; ++th
) {
279 cl_git_pass(git_thread_join(&t
[th
], &rval
));
280 cl_assert_equal_i(th
, *((int *)rval
));
284 git_repository_free(g_repo
);