Skip to content

Commit 836c177

Browse files
committed
fix: ensure ordering while handling map from resp3
Signed-off-by: Anthony Griffon <[email protected]>
1 parent 3aa2584 commit 836c177

File tree

4 files changed

+8
-13
lines changed

4 files changed

+8
-13
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/roster/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ derive_builder.workspace = true
3535
dotenv.workspace = true
3636
futures = "0.3"
3737
futures-locks = "0.7"
38+
indexmap = "2"
3839
local-sync = "0.1"
3940
monoio = { workspace = true, features = ["bytes", "sync", "iouring"] }
4041
rustc-hash = "1.1.0"

app/roster/src/application/server/frame/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! Provides a type representing a Redis protocol frame as well as utilities for
22
//! parsing frames from a byte array.
33
4-
use std::collections::HashMap;
54
use std::convert::TryInto;
65
use std::io::Cursor;
76
use std::num::TryFromIntError;
87
use std::string::FromUtf8Error;
98

109
use bytes::{Buf, Bytes, BytesMut};
1110
use bytestring::ByteString;
11+
use indexmap::IndexMap;
1212

1313
pub(crate) mod write;
1414

@@ -21,7 +21,7 @@ pub enum Frame {
2121
Bulk(Bytes),
2222
Null,
2323
Array(Vec<Frame>),
24-
Map(HashMap<Frame, Frame>),
24+
Map(IndexMap<Frame, Frame>),
2525
}
2626

2727
// TODO(@miaxos): hacky hash derivation for now, to test a little.
@@ -182,7 +182,7 @@ impl Frame {
182182
}
183183
b'%' => {
184184
let len = get_decimal(src)?.try_into()?;
185-
let mut out = HashMap::with_capacity(len);
185+
let mut out = IndexMap::with_capacity(len);
186186

187187
for _ in (0..(len * 2)).step_by(2) {
188188
let key = Frame::parse(src)?;

app/roster/src/application/server/frame/write.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ pub async fn write_frame(
9797

9898
#[cfg(test)]
9999
mod tests {
100-
use std::collections::HashMap;
101100
use std::io::Write;
102101

103102
use bytestring::ByteString;
103+
use indexmap::IndexMap;
104104
use monoio::buf::{IoBuf, IoVecBuf};
105105
use monoio::io::AsyncWriteRent;
106106
use monoio::BufResult;
@@ -176,9 +176,7 @@ mod tests {
176176
#[monoio::test]
177177
async fn simple_decimal_write_value_hashmap() {
178178
let mut v = TestUtilVec(Vec::new());
179-
// HashMap are unordered when accessing them, so it could be first first
180-
// or first in second.
181-
let frame = Frame::Map(HashMap::from_iter([
179+
let frame = Frame::Map(IndexMap::from_iter([
182180
(
183181
Frame::Simple(ByteString::from_static("first")),
184182
Frame::Integer(1),
@@ -189,11 +187,6 @@ mod tests {
189187
),
190188
]));
191189
write_frame(&mut v, &frame).await.unwrap();
192-
let possible_1 =
193-
String::from_utf8_lossy(b"%2\r\n+first\r\n:1\r\n+second\r\n:2\r\n");
194-
let possible_2 =
195-
String::from_utf8_lossy(b"%2\r\n+second\r\n:2\r\n+first\r\n:1\r\n");
196-
let result = String::from_utf8(v.0).unwrap();
197-
assert!(result == possible_1 || result == possible_2);
190+
insta::assert_debug_snapshot!(String::from_utf8(v.0).unwrap(), @r###""%2\r\n+first\r\n:1\r\n+second\r\n:2\r\n""###);
198191
}
199192
}

0 commit comments

Comments
 (0)