@@ -697,15 +697,15 @@ TEST(PersistenceLayer, FileSafeVsUnsafeIterators) {
697
697
struct_schema.AddType <StorableString>();
698
698
const std::string signature =
699
699
" #signature " + JSON (current::ss::StreamSignature (namespace_name, struct_schema.GetSchemaInfo ())) + ' \n ' ;
700
- std::string lines[] = {" {\" index\" :0,\" us\" :100}\t {\" s\" :\" foo\" }\n " ,
701
- " {\" index\" :1,\" us\" :200}\t {\" s\" :\" bar\" }\n " ,
702
- " #head 00000000000000000300\n " ,
703
- " {\" index\" :2,\" us\" :500}\t {\" s\" :\" meh\" }\n " ,
704
- " #head 00000000000000000600\n " };
700
+ std::vector<std:: string> lines = {" {\" index\" :0,\" us\" :100}\t {\" s\" :\" foo\" }" ,
701
+ " {\" index\" :1,\" us\" :200}\t {\" s\" :\" bar\" }" ,
702
+ " #head 00000000000000000300" ,
703
+ " {\" index\" :2,\" us\" :500}\t {\" s\" :\" meh\" }" ,
704
+ " #head 00000000000000000600" };
705
705
const auto CombineFileContents = [&]() -> std::string {
706
706
std::string contents = signature;
707
707
for (const auto & line : lines) {
708
- contents += line;
708
+ contents += line + ' \n ' ;
709
709
}
710
710
return contents;
711
711
};
@@ -715,12 +715,12 @@ TEST(PersistenceLayer, FileSafeVsUnsafeIterators) {
715
715
std::mutex mutex;
716
716
IMPL impl (mutex, namespace_name, persistence_file_name);
717
717
718
- const auto CheckUnsafeIteration = [&]() {
719
- std::vector<std:: string> all_entries_unsafe ;
718
+ const auto GetUnsafeIterationResult = [&]() -> std::string {
719
+ std::string combined_result ;
720
720
for (const auto & e : impl.Iterate <current::ss::IterationMode::Unsafe>()) {
721
- all_entries_unsafe. push_back (e) ;
721
+ combined_result += e + ' \n ' ;
722
722
}
723
- EXPECT_EQ (lines[ 0 ] + lines[ 1 ] + lines[ 3 ], Join (all_entries_unsafe, " \n " ) + ' \n ' ) ;
723
+ return combined_result ;
724
724
};
725
725
726
726
{
@@ -730,7 +730,11 @@ TEST(PersistenceLayer, FileSafeVsUnsafeIterators) {
730
730
" %s %d %d" , e.entry .s .c_str (), static_cast <int >(e.idx_ts .index ), static_cast <int >(e.idx_ts .us .count ())));
731
731
}
732
732
EXPECT_EQ (" foo 0 100,bar 1 200,meh 2 500" , Join (all_entries, " ," ));
733
- CheckUnsafeIteration ();
733
+ EXPECT_EQ (
734
+ " {\" index\" :0,\" us\" :100}\t {\" s\" :\" foo\" }\n "
735
+ " {\" index\" :1,\" us\" :200}\t {\" s\" :\" bar\" }\n "
736
+ " {\" index\" :2,\" us\" :500}\t {\" s\" :\" meh\" }\n " ,
737
+ GetUnsafeIterationResult ());
734
738
}
735
739
736
740
{
@@ -740,7 +744,7 @@ TEST(PersistenceLayer, FileSafeVsUnsafeIterators) {
740
744
EXPECT_NO_THROW (*(++impl.Iterate ().begin ()));
741
745
742
746
// swap the first two entries to provoke the InconsistentIndexException exceptions.
743
- lines[0 ]. swap ( lines[1 ]);
747
+ std::swap ( lines[0 ], lines[1 ]);
744
748
current::FileSystem::WriteStringToFile (CombineFileContents (), persistence_file_name.c_str ());
745
749
746
750
// Check that "safe" iterators throw the InconsistentIndexException while "unsafe" ones don't.
@@ -752,21 +756,29 @@ TEST(PersistenceLayer, FileSafeVsUnsafeIterators) {
752
756
EXPECT_NO_THROW (*impl.Iterate <current::ss::IterationMode::Unsafe>(1 , 2 ).begin ());
753
757
EXPECT_NO_THROW (*impl.Iterate <current::ss::IterationMode::Unsafe>().begin ());
754
758
EXPECT_NO_THROW (*(++impl.Iterate <current::ss::IterationMode::Unsafe>().begin ()));
755
- CheckUnsafeIteration ();
756
- lines[0 ].swap (lines[1 ]);
759
+ EXPECT_EQ (
760
+ " {\" index\" :1,\" us\" :200}\t {\" s\" :\" bar\" }\n "
761
+ " {\" index\" :0,\" us\" :100}\t {\" s\" :\" foo\" }\n "
762
+ " {\" index\" :2,\" us\" :500}\t {\" s\" :\" meh\" }\n " ,
763
+ GetUnsafeIterationResult ());
764
+ std::swap (lines[0 ], lines[1 ]);
757
765
}
758
766
759
767
{
760
- // Produce wrong JSON instead of an entry: replace all symbols, except last \n one .
761
- lines[1 ].replace (0 , lines[1 ].length () - 1 , lines[1 ].length () - 1 , ' A' );
768
+ // Produce wrong JSON instead of an entire entry - replace both index and value with garbage .
769
+ lines[1 ].replace (0 , lines[1 ].length (), lines[1 ].length (), ' A' );
762
770
current::FileSystem::WriteStringToFile (CombineFileContents (), persistence_file_name.c_str ());
763
771
764
772
// Check that "safe" iterators throw the MalformedEntryException while "unsafe" ones don't.
765
773
EXPECT_THROW (*(++(impl.Iterate ().begin ())), current::persistence::MalformedEntryException);
766
774
EXPECT_THROW (*impl.Iterate (1 , 2 ).begin (), current::persistence::MalformedEntryException);
767
775
EXPECT_NO_THROW (*impl.Iterate <current::ss::IterationMode::Unsafe>(1 , 2 ).begin ());
768
776
EXPECT_NO_THROW (*(++impl.Iterate <current::ss::IterationMode::Unsafe>().begin ()));
769
- CheckUnsafeIteration ();
777
+ EXPECT_EQ (
778
+ " {\" index\" :0,\" us\" :100}\t {\" s\" :\" foo\" }\n "
779
+ " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n "
780
+ " {\" index\" :2,\" us\" :500}\t {\" s\" :\" meh\" }\n " ,
781
+ GetUnsafeIterationResult ());
770
782
}
771
783
772
784
{
@@ -783,7 +795,11 @@ TEST(PersistenceLayer, FileSafeVsUnsafeIterators) {
783
795
EXPECT_THROW (*impl.Iterate (0 , 1 ).begin (), current::serialization::json::InvalidJSONException);
784
796
EXPECT_THROW (*impl.Iterate ().begin (), current::serialization::json::InvalidJSONException);
785
797
EXPECT_NO_THROW (*impl.Iterate <current::ss::IterationMode::Unsafe>(0 , 1 ).begin ());
786
- CheckUnsafeIteration ();
798
+ EXPECT_EQ (
799
+ " BBBBBBBBBBBBBBBBBBBB\t {\" s\" :\" foo\" }\n "
800
+ " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n "
801
+ " {\" index\" :2,\" us\" :500}\t {\" s\" :\" meh\" }\n " ,
802
+ GetUnsafeIterationResult ());
787
803
}
788
804
789
805
{
@@ -792,14 +808,18 @@ TEST(PersistenceLayer, FileSafeVsUnsafeIterators) {
792
808
// Produce wrong JSON instead of the entry's value only and leave index and timestamp section valid.
793
809
const auto tab_pos = lines[3 ].find (' \t ' );
794
810
ASSERT_FALSE (std::string::npos == tab_pos);
795
- const auto length_to_replace = ( lines[3 ].length () - 1 ) - (tab_pos + 1 );
811
+ const auto length_to_replace = lines[3 ].length () - (tab_pos + 1 );
796
812
lines[3 ].replace (tab_pos + 1 , length_to_replace, length_to_replace, ' C' );
797
813
current::FileSystem::WriteStringToFile (CombineFileContents (), persistence_file_name.c_str ());
798
814
799
815
// Check that "safe" iterators throw the InvalidJSONException while "unsafe" ones don't.
800
816
EXPECT_THROW (*impl.Iterate (2 , 3 ).begin (), current::serialization::json::InvalidJSONException);
801
817
EXPECT_NO_THROW (*impl.Iterate <current::ss::IterationMode::Unsafe>(2 , 3 ).begin ());
802
- CheckUnsafeIteration ();
818
+ EXPECT_EQ (
819
+ " BBBBBBBBBBBBBBBBBBBB\t {\" s\" :\" foo\" }\n "
820
+ " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n "
821
+ " {\" index\" :2,\" us\" :500}\t CCCCCCCCCCC\n " ,
822
+ GetUnsafeIterationResult ());
803
823
}
804
824
}
805
825
0 commit comments