diff --git a/rust/src/pool.rs b/rust/src/pool.rs index c7dd89926..695cc079a 100644 --- a/rust/src/pool.rs +++ b/rust/src/pool.rs @@ -664,6 +664,46 @@ mod tests { assert_eq!(count.load(Ordering::Relaxed), 16); } + unsafe extern "C" fn delayed_increment(opaque: *mut c_void) { + thread::sleep(Duration::from_millis(10)); + unsafe { increment(opaque) }; + } + + #[test] + fn shrinking_the_limit_keeps_draining_a_small_queue() { + let ctx = POOL_create(4, 2); + assert!(!ctx.is_null()); + let count = AtomicUsize::new(0); + + for _ in 0..16 { + unsafe { + POOL_add( + ctx, + delayed_increment, + ptr::from_ref(&count).cast_mut().cast(), + ) + }; + } + unsafe { POOL_joinJobs(ctx) }; + assert_eq!(count.load(Ordering::Relaxed), 16); + + assert_eq!(unsafe { POOL_resize(ctx, 2) }, 0); + for _ in 0..16 { + unsafe { + POOL_add( + ctx, + delayed_increment, + ptr::from_ref(&count).cast_mut().cast(), + ) + }; + } + unsafe { + POOL_joinJobs(ctx); + POOL_free(ctx); + } + assert_eq!(count.load(Ordering::Relaxed), 32); + } + struct AllocStats { allocations: AtomicUsize, frees: AtomicUsize, diff --git a/tests/Makefile b/tests/Makefile index e9f4ac553..85b0db790 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -324,8 +324,9 @@ decodecorpus : LDLIBS += -lm decodecorpus : $(filter-out zstdc_zstd_compress.o, $(ZSTD_OBJECTS)) $(ZDICT_FILES) $(PRGDIR)/util.c $(PRGDIR)/timefn.c decodecorpus.c CLEAN += poolTests -poolTests : $(PRGDIR)/util.c $(PRGDIR)/timefn.c poolTests.c $(LIB_SRCDIR)/common/pool.c $(LIB_SRCDIR)/common/threading.c $(LIB_SRCDIR)/common/zstd_common.c $(LIB_SRCDIR)/common/error_private.c - $(LINK.c) $(MULTITHREAD) $^ -o $@$(EXT) +poolTests : CPPFLAGS += -D_POSIX_C_SOURCE=200809L $(MULTITHREAD_CPP) +poolTests : $(ZSTDMT_OBJECTS) $(PRGDIR)/util.c $(PRGDIR)/timefn.c poolTests.c + $(LINK.c) $(MULTITHREAD_LD) $^ -o $@$(EXT) # These static C test executables exercise Rust replacements. The normal # prerequisite is also an archive input, placed after C sources by `$^`, and diff --git a/tests/poolTests.c b/tests/poolTests.c index 9e62722bf..6a0793c5b 100644 --- a/tests/poolTests.c +++ b/tests/poolTests.c @@ -15,6 +15,7 @@ #include "timefn.h" #include #include +#include #define ASSERT_TRUE(p) \ do { \