Skip to content

Commit

Permalink
fix: checker types ser/de panic
Browse files Browse the repository at this point in the history
Signed-off-by: lxl66566 <lxl66566@gmail.com>
  • Loading branch information
lxl66566 committed Sep 25, 2024
1 parent 6748ad1 commit 7485720
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 96 deletions.
39 changes: 39 additions & 0 deletions assets/check_result.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{:valid? false,
:anomaly-types [:G1c],
:anomalies
{:G1c
[{:cycle
[{:process 1,
:type :ok,
:f nil,
:value [[:append :x 2] [:append :y 1]],
:index 1,
:time -1}
{:process 0,
:type :ok,
:f nil,
:value [[:append :x 1] [:r :y [1]]],
:index 0,
:time -1}
{:process 1,
:type :ok,
:f nil,
:value [[:append :x 2] [:append :y 1]],
:index 1,
:time -1}],
:steps
({:type :wr, :key :y, :value 1, :a-mop-index 1, :b-mop-index 1}
{:type :ww,
:key :x,
:value 1,
:value' 2,
:a-mop-index 0,
:b-mop-index 0}),
:type :G1c}]},
:not #{:read-committed},
:also-not
#{:consistent-view :cursor-stability :forward-consistent-view
:monotonic-atomic-view :monotonic-snapshot-read :monotonic-view
:repeatable-read :serializable :snapshot-isolation :strong-serializable
:strong-session-serializable :strong-session-snapshot-isolation
:strong-snapshot-isolation :update-serializable}}
74 changes: 0 additions & 74 deletions assets/check_result.json

This file was deleted.

4 changes: 2 additions & 2 deletions src/checker/elle_rw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl super::Check for ElleRwChecker {
with_jvm(|_| {
let h = historify(Instance::from_ser(history)?)?;
trace!("historify done");
info!("check with option: {:?}", serde_json::to_string(&option));
info!("check with option: {:?}", &option);
let op_clj = Instance::from_ser(option)?;
let res = nsinvoke!(self.ns, "check", op_clj, h)?;
trace!("check done");
Expand All @@ -60,7 +60,7 @@ mod tests {
let res = checker.check(
&history,
CheckOption::default()
.consistency_models(ConsistencyModel::Serializable)
.consistency_models([ConsistencyModel::Serializable])
.analyzer("wr-graph"),
)?;
println!("{:#?}", res);
Expand Down
40 changes: 25 additions & 15 deletions src/checker/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod elle_rw;
use std::path::PathBuf;
use std::{collections::HashSet, path::PathBuf};

use anyhow::Result;
use default_struct_builder::DefaultBuilder;
Expand All @@ -17,14 +17,14 @@ fn default_out_dir() -> PathBuf {
pub struct SerializableCheckResult {
#[serde(rename = ":valid?")]
valid: ValidType,
#[serde(rename = ":anomaly-types")]
#[serde(rename = ":anomaly-types", default)]
anomaly_types: Vec<String>,
#[serde(rename = ":anomalies")]
anomalies: serde_json::Value,
#[serde(rename = ":not")]
not: Vec<String>,
#[serde(rename = ":also-not")]
also_not: Vec<String>,
anomalies: Option<serde_json::Value>,
#[serde(rename = ":not", default)]
not: HashSet<String>,
#[serde(rename = ":also-not", default)]
also_not: HashSet<String>,
}

#[serde_with::skip_serializing_none]
Expand All @@ -33,7 +33,7 @@ pub struct SerializableCheckResult {
pub struct CheckOption {
#[builder(into)]
#[serde(rename = ":consistency-models")]
consistency_models: Option<ConsistencyModel>,
consistency_models: Option<Vec<ConsistencyModel>>,
#[serde(default = "default_out_dir")]
#[serde(rename = ":directory")]
directory: PathBuf,
Expand Down Expand Up @@ -73,7 +73,7 @@ impl<'de> Deserialize<'de> for ValidType {
match value {
Value::Bool(b) => Ok(if b { ValidType::True } else { ValidType::False }),
Value::String(s) => {
if s == "unknown" {
if s == ":unknown" {
Ok(ValidType::Unknown)
} else {
Err(serde::de::Error::custom("invalid string value"))
Expand All @@ -92,7 +92,7 @@ impl Serialize for ValidType {
match self {
ValidType::True => serializer.serialize_bool(true),
ValidType::False => serializer.serialize_bool(false),
ValidType::Unknown => serializer.serialize_str("unknown"),
ValidType::Unknown => serializer.serialize_str(":unknown"),
}
}
}
Expand Down Expand Up @@ -153,23 +153,33 @@ pub trait Check {
#[cfg(test)]
mod tests {
use super::*;
use crate::ffi::{read_edn, ToDe};

#[test]
fn test_deser_from_json_result() -> anyhow::Result<()> {
let json = include_str!("../../assets/check_result.json");
let _res: SerializableCheckResult = serde_json::from_str(json)?;
fn test_deser_from_json_result_should_be_ok() -> anyhow::Result<()> {
let edn = include_str!("../../assets/check_result.edn");
let ins = read_edn(edn)?;
let _res: SerializableCheckResult = ins.to_de()?;
Ok(())
}

#[test]
fn test_check_option_serialization() {
let option = CheckOption::default()
.analyzer("wr-graph")
.consistency_models(ConsistencyModel::CursorStability);
.consistency_models([ConsistencyModel::CursorStability]);
let json = serde_json::to_string(&option).unwrap();
assert_eq!(
r#"{":consistency-models":":cursor-stability",":directory":"./out",":analyzer":"wr-graph"}"#,
r#"{":consistency-models":[":cursor-stability"],":directory":"./out",":analyzer":"wr-graph"}"#,
json
);
}

#[test]
fn test_check_result_deserialization() -> anyhow::Result<()> {
let result = r#"{":valid?":":unknown",":anomaly-types":[":empty-transaction-graph"],":anomalies":{":empty-transaction-graph":true},":not":[],":also-not":[]}"#;
let res: SerializableCheckResult = serde_json::from_str(result)?;
assert!(matches!(res.valid, ValidType::Unknown));
Ok(())
}
}
13 changes: 8 additions & 5 deletions src/clojure/serde.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@
x))
data)))


(defn deserialize-with-key-type [json-str]
(let [data (json/parse-string json-str)]
(walk/postwalk
(fn [x]
(if (string? x)
(if (.startsWith x ":")
(keyword (subs x 1))
x)
x))
(cond
(string? x) (if (.startsWith x ":")
(keyword (subs x 1))
x)
(number? x) (long x) ;; if this line not there, it will deserialize to Integer,
;; that causes problem in history.
:else x))
data)))

(defn deserialize-list-to-vec [json-str]
Expand Down

0 comments on commit 7485720

Please sign in to comment.