File: sync_set_once.rs

package info (click to toggle)
rust-tokio 1.48.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,444 kB
  • sloc: makefile: 2
file content (180 lines) | stat: -rw-r--r-- 3,653 bytes parent folder | download | duplicates (3)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#![warn(rust_2018_idioms)]
#![cfg(feature = "full")]

use std::sync::{
    atomic::{AtomicU32, Ordering},
    Arc,
};
use tokio::sync::SetOnce;

#[derive(Clone)]
struct DropCounter {
    drops: Arc<AtomicU32>,
}

impl DropCounter {
    fn new() -> Self {
        DropCounter {
            drops: Arc::new(AtomicU32::new(0)),
        }
    }

    fn assert_num_drops(&self, value: u32) {
        assert_eq!(value, self.drops.load(Ordering::Relaxed));
    }
}

impl Drop for DropCounter {
    fn drop(&mut self) {
        self.drops.fetch_add(1, Ordering::Relaxed);
    }
}

#[test]
fn drop_cell() {
    let fooer = DropCounter::new();
    let fooer_cl = fooer.clone();

    {
        let once_cell = SetOnce::new();
        let prev = once_cell.set(fooer_cl);
        assert!(prev.is_ok())
    }

    fooer.assert_num_drops(1);
}

#[test]
fn drop_cell_new_with() {
    let fooer = DropCounter::new();

    {
        let once_cell = SetOnce::new_with(Some(fooer.clone()));
        assert!(once_cell.initialized());
    }

    fooer.assert_num_drops(1);
}

#[test]
fn drop_into_inner() {
    let fooer = DropCounter::new();

    let once_cell = SetOnce::new();
    assert!(once_cell.set(fooer.clone()).is_ok());
    let val = once_cell.into_inner();
    fooer.assert_num_drops(0);
    drop(val);
    fooer.assert_num_drops(1);
}

#[test]
fn drop_into_inner_new_with() {
    let fooer = DropCounter::new();

    let once_cell = SetOnce::new_with(Some(fooer.clone()));
    let val = once_cell.into_inner();
    fooer.assert_num_drops(0);
    drop(val);
    fooer.assert_num_drops(1);
}

#[test]
fn from() {
    let cell = SetOnce::from(2);
    assert_eq!(*cell.get().unwrap(), 2);
}

#[test]
fn set_and_get() {
    static ONCE: SetOnce<u32> = SetOnce::const_new();

    ONCE.set(5).unwrap();
    let value = ONCE.get().unwrap();
    assert_eq!(*value, 5);
}

#[tokio::test]
async fn set_and_wait() {
    static ONCE: SetOnce<u32> = SetOnce::const_new();

    tokio::spawn(async { ONCE.set(5) });

    let value = ONCE.wait().await;
    assert_eq!(*value, 5);
}

#[test]
#[cfg_attr(target_family = "wasm", ignore)]
fn set_and_wait_multiple_threads() {
    static ONCE: SetOnce<u32> = SetOnce::const_new();

    let res1 = std::thread::spawn(|| ONCE.set(4));

    let res2 = std::thread::spawn(|| ONCE.set(3));

    let result_first = res1.join().unwrap().is_err();
    let result_two = res2.join().unwrap().is_err();

    assert!(result_first != result_two);
}

#[tokio::test]
#[cfg_attr(target_family = "wasm", ignore)]
async fn set_and_wait_threads() {
    static ONCE: SetOnce<u32> = SetOnce::const_new();

    let thread = std::thread::spawn(|| {
        ONCE.set(4).unwrap();
    });

    let value = ONCE.wait().await;
    thread.join().unwrap();
    assert_eq!(*value, 4);
}

#[test]
fn get_uninit() {
    static ONCE: SetOnce<u32> = SetOnce::const_new();
    let uninit = ONCE.get();
    assert!(uninit.is_none());
}

#[test]
fn set_twice() {
    static ONCE: SetOnce<u32> = SetOnce::const_new();

    let first = ONCE.set(5);
    assert_eq!(first, Ok(()));
    let second = ONCE.set(6);
    assert!(second.is_err());
}

#[test]
fn is_none_initializing() {
    static ONCE: SetOnce<u32> = SetOnce::const_new();

    assert_eq!(ONCE.get(), None);

    ONCE.set(20).unwrap();

    assert!(ONCE.set(10).is_err());
}

#[tokio::test]
async fn is_some_initializing() {
    static ONCE: SetOnce<u32> = SetOnce::const_new();

    tokio::spawn(async { ONCE.set(20) });

    assert_eq!(*ONCE.wait().await, 20);
}

#[test]
fn into_inner_int_empty_setonce() {
    let once = SetOnce::<u32>::new();

    let val = once.into_inner();

    assert!(val.is_none());
}