Skip to content

Commit

Permalink
test: fix test add/remove
Browse files Browse the repository at this point in the history
  • Loading branch information
Zxilly committed Dec 18, 2024
1 parent 29f3f8a commit 45946bd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ on:
jobs:
test:
name: Run unit tests
runs-on: ubuntu-22.04
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
Expand Down Expand Up @@ -77,7 +77,7 @@ jobs:
test-integration:
name: Run integration tests
runs-on: ubuntu-22.04
runs-on: ubuntu-latest
strategy:
matrix:
cache: [ "-DUA2F_NO_CACHE=ON", "-DUA2F_NO_CACHE=OFF" ]
Expand Down
18 changes: 9 additions & 9 deletions src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@

pthread_rwlock_t cacheLock;

struct cache *not_http_dst_cache = NULL;
struct cache *dst_cache = NULL;

typedef struct {
int check_interval;
pthread_rwlock_t* lock;
struct cache* table;
struct cache** table;
} thread_args;

_Noreturn static void* check_cache(void* arg) {
const __auto_type args = (thread_args *)arg;
const __auto_type lock = args->lock;
const __auto_type table = args->table;
const __auto_type check_interval = args->check_interval;
const __auto_type cache_ptr = args->table;
free(args);

while (true) {
Expand All @@ -34,9 +34,9 @@ _Noreturn static void* check_cache(void* arg) {
const time_t now = time(NULL);
struct cache *cur, *tmp;

HASH_ITER(hh, table, cur, tmp) {
HASH_ITER(hh, *cache_ptr, cur, tmp) {
if (difftime(now, cur->last_time) > check_interval) {
HASH_DEL(not_http_dst_cache, cur);
HASH_DEL(*cache_ptr, cur);
free(cur);
}
}
Expand Down Expand Up @@ -67,7 +67,7 @@ void init_not_http_cache(const int interval) {
}
arg->check_interval = interval;
arg->lock = &cacheLock;
arg->table = not_http_dst_cache;
arg->table = &dst_cache;

const __auto_type ret = pthread_create(&cleanup_thread, &cleanup_thread_attr, check_cache, arg);
if (ret) {
Expand All @@ -81,7 +81,7 @@ bool cache_contains(struct addr_port target) {
pthread_rwlock_wrlock(&cacheLock);

struct cache *s;
HASH_FIND(hh, not_http_dst_cache, &target, sizeof(struct addr_port), s);
HASH_FIND(hh, dst_cache, &target, sizeof(struct addr_port), s);
if (s != NULL) {
s->last_time = time(NULL);
}
Expand All @@ -96,11 +96,11 @@ void cache_add(struct addr_port addr_port) {

struct cache *s;

HASH_FIND(hh, not_http_dst_cache, &addr_port, sizeof(struct addr_port), s);
HASH_FIND(hh, dst_cache, &addr_port, sizeof(struct addr_port), s);
if (s == NULL) {
s = malloc(sizeof(struct cache));
memcpy(&s->target.addr, &addr_port, sizeof(struct addr_port));
HASH_ADD(hh, not_http_dst_cache, target.addr, sizeof(struct addr_port), s);
HASH_ADD(hh, dst_cache, target.addr, sizeof(struct addr_port), s);
}
s->last_time = time(NULL);

Expand Down
2 changes: 1 addition & 1 deletion src/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct cache {
UT_hash_handle hh;
};

extern struct cache *not_http_dst_cache;
extern struct cache *dst_cache;
extern pthread_rwlock_t cacheLock;

void init_not_http_cache(int interval);
Expand Down
15 changes: 11 additions & 4 deletions test/cache_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

extern "C" {
#include <cache.h>
#include <syslog.h>
}

class CacheTest : public ::testing::Test {
Expand All @@ -10,14 +11,20 @@ class CacheTest : public ::testing::Test {

static void SetUpTestSuite() {
init_not_http_cache(1);

// redirect syslog to stderr
openlog("ua2f", LOG_PID | LOG_PERROR, LOG_USER);
}

static void TearDownTestSuite() {
closelog();
}

void TearDown() override {
pthread_rwlock_wrlock(&cacheLock);
// Clear the cache after each test
cache *cur, *tmp;
HASH_ITER(hh, not_http_dst_cache, cur, tmp) {
HASH_DEL(not_http_dst_cache, cur);
HASH_ITER(hh, dst_cache, cur, tmp) {
HASH_DEL(dst_cache, cur);
free(cur);
}
pthread_rwlock_unlock(&cacheLock);
Expand All @@ -34,7 +41,7 @@ TEST_F(CacheTest, AddToCache) {
TEST_F(CacheTest, AddAndRemoveFromCache) {
cache_add(test_addr);
EXPECT_TRUE(cache_contains(test_addr));
sleep(5);
sleep(3);
EXPECT_FALSE(cache_contains(test_addr));
}

Expand Down

0 comments on commit 45946bd

Please sign in to comment.