-
-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathshmem_tests.rs
120 lines (94 loc) · 3.17 KB
/
shmem_tests.rs
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//LICENSE Portions Copyright 2019-2021 ZomboDB, LLC.
//LICENSE
//LICENSE Portions Copyright 2021-2023 Technology Concepts & Design, Inc.
//LICENSE
//LICENSE Portions Copyright 2023-2023 PgCentral Foundation, Inc. <[email protected]>
//LICENSE
//LICENSE All rights reserved.
//LICENSE
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
use pgrx::prelude::*;
use pgrx::{pg_shmem_init, PgAtomic, PgLwLock, PgSharedMemoryInitialization};
use std::sync::atomic::AtomicBool;
#[cfg(feature = "cshim")]
use pgrx::PgHashMap;
static ATOMIC: PgAtomic<AtomicBool> = PgAtomic::new();
static LWLOCK: PgLwLock<bool> = PgLwLock::new();
#[cfg(feature = "cshim")]
static HASH_MAP: PgHashMap<i64, i64> = PgHashMap::new(500);
#[pg_guard]
pub extern "C" fn _PG_init() {
// This ensures that this functionality works across PostgreSQL versions
pg_shmem_init!(ATOMIC);
pg_shmem_init!(LWLOCK);
#[cfg(feature = "cshim")]
pg_shmem_init!(HASH_MAP);
}
#[cfg(any(test, feature = "pg_test"))]
#[pgrx::pg_schema]
mod tests {
#[allow(unused_imports)]
use crate as pgrx_tests;
#[cfg(feature = "cshim")]
use crate::tests::shmem_tests::HASH_MAP;
use crate::tests::shmem_tests::LWLOCK;
use pgrx::prelude::*;
#[pg_test]
#[should_panic(expected = "cache lookup failed for type 0")]
pub fn test_behaves_normally_when_elog_while_holding_lock() {
// Hold lock
let _lock = LWLOCK.exclusive();
// Call into pg_guarded postgres function which internally reports an error
unsafe { pg_sys::format_type_extended(pg_sys::InvalidOid, -1, 0) };
}
#[pg_test]
pub fn test_lock_is_released_on_drop() {
let lock = LWLOCK.exclusive();
drop(lock);
let _lock = LWLOCK.exclusive();
}
#[pg_test]
pub fn test_lock_is_released_on_unwind() {
let _res = std::panic::catch_unwind(|| {
let _lock = LWLOCK.exclusive();
panic!("get out")
});
let _lock = LWLOCK.exclusive();
}
#[cfg(feature = "cshim")]
#[pg_test]
pub fn test_pg_hash_map() {
use rand::prelude::IteratorRandom;
for i in 1..250 {
assert_eq!(HASH_MAP.insert(i, i), Ok(None));
}
assert_eq!(HASH_MAP.len(), 249);
for i in 1..250 {
assert_eq!(HASH_MAP.get(i), Some(i));
}
assert_eq!(HASH_MAP.len(), 249);
for i in 251..500 {
assert_eq!(HASH_MAP.get(i), None);
}
assert_eq!(HASH_MAP.len(), 249);
for i in 1..250 {
assert_eq!(HASH_MAP.insert(i, i), Ok(Some(i)));
}
assert_eq!(HASH_MAP.len(), 249);
for i in 1..250 {
assert_eq!(HASH_MAP.remove(i), Some(i));
}
assert_eq!(HASH_MAP.len(), 0);
for i in 1..250 {
assert_eq!(HASH_MAP.get(i), None);
}
assert_eq!(HASH_MAP.len(), 0);
for _ in 0..25_000 {
for key in 0..250 {
let value = (0..1000).choose(&mut rand::thread_rng()).unwrap();
assert!(HASH_MAP.insert(key, value).is_ok());
}
}
assert_eq!(HASH_MAP.len(), 250);
}
}