-
Notifications
You must be signed in to change notification settings - Fork 1.1k
167 lines (143 loc) · 5.29 KB
/
test-rust.yml
File metadata and controls
167 lines (143 loc) · 5.29 KB
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
name: Rust Extension Tests
on:
push:
branches: ["master", "v**"]
pull_request:
workflow_dispatch:
concurrency:
group: rust-tests-${{ github.ref }}
cancel-in-progress: true
defaults:
run:
shell: bash -eux {0}
permissions:
contents: read
jobs:
build-and-test:
name: Rust Extension - ${{ matrix.os }} - Python ${{ matrix.python-version }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
exclude:
# Reduce matrix size - test all Python versions on Linux, subset on others
- os: macos-latest
python-version: "3.10"
- os: macos-latest
python-version: "3.11"
- os: windows-latest
python-version: "3.10"
- os: windows-latest
python-version: "3.11"
steps:
- name: Checkout code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
- name: Set up Python
uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5.3.0
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@7b1c307e0dcbda6122208f10795a713336a9b35a # stable
with:
toolchain: stable
- name: Cache Rust dependencies
uses: actions/cache@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
bson/_rbson/target/
key: ${{ runner.os }}-cargo-${{ hashFiles('bson/_rbson/Cargo.lock') }}
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
pip install maturin pytest
- name: Build C extension
run: |
pip install setuptools
python _setup.py build_ext -i
- name: Build Rust extension
run: |
cd bson/_rbson
bash build.sh
- name: Verify both extensions are available
run: |
python -c "
import bson
print(f'C extension available: {bson.has_c()}')
print(f'Rust extension available: {bson.has_rust()}')
assert bson.has_c(), 'C extension should be available'
assert bson.has_rust(), 'Rust extension should be available'
"
- name: Smoke test - C extension (default)
run: |
python -c "
import bson
assert bson.get_bson_implementation() == 'c', 'Should default to C'
data = {'test': 'c_extension', 'value': 42}
encoded = bson.encode(data)
decoded = bson.decode(encoded)
assert decoded == data, 'C extension encode/decode failed'
print('C extension smoke test passed')
"
- name: Smoke test - Rust extension
env:
PYMONGO_USE_RUST: "1"
run: |
python -c "
import bson
assert bson.get_bson_implementation() == 'rust', 'Should use Rust'
data = {'test': 'rust_extension', 'value': 99, 'nested': {'key': 'value'}}
encoded = bson.encode(data)
decoded = bson.decode(encoded)
assert decoded == data, 'Rust extension encode/decode failed'
print('Rust extension smoke test passed')
"
- name: Run BSON test suite with C extension (baseline)
run: |
python -m unittest test.test_bson.TestBSON -v
- name: Run BSON test suite with Rust extension
env:
PYMONGO_USE_RUST: "1"
run: |
python -m unittest test.test_bson.TestBSON -v
- name: Test cross-compatibility (C → Rust)
run: |
python -c "
import os
import sys
# Encode with C
import bson as bson_c
assert bson_c.get_bson_implementation() == 'c'
data = {'cross': 'compatibility', 'test': True}
c_encoded = bson_c.encode(data)
# Decode with Rust - preserve extension modules
os.environ['PYMONGO_USE_RUST'] = '1'
# Save extension modules before clearing
_cbson = sys.modules.get('bson._cbson')
_rbson = sys.modules.get('bson._rbson')
# Clear bson modules except extensions
for key in list(sys.modules.keys()):
if key.startswith('bson') and not key.endswith(('_cbson', '_rbson')):
del sys.modules[key]
# Restore extension modules
if _cbson:
sys.modules['bson._cbson'] = _cbson
if _rbson:
sys.modules['bson._rbson'] = _rbson
import bson as bson_rust
assert bson_rust.get_bson_implementation() == 'rust'
rust_decoded = bson_rust.decode(c_encoded)
assert rust_decoded == data, 'Cross-compatibility failed'
print('C to Rust cross-compatibility works')
"
- name: Run performance benchmark
run: |
FASTBENCH=1 python test/performance/perf_test.py TestRustSimpleIntEncodingC TestRustSimpleIntEncodingRust TestRustMixedTypesEncodingC TestRustMixedTypesEncodingRust -v