|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use crate::streaming::partitions::storage::{load_consumer_group_offsets, load_consumer_offsets}; |
| 19 | +use iggy_common::{ConsumerKind, IggyError}; |
| 20 | +use std::path::Path; |
| 21 | +use std::sync::atomic::Ordering; |
| 22 | + |
| 23 | +fn write_offset_file(dir: &Path, name: &str, offset: u64) { |
| 24 | + std::fs::write(dir.join(name), offset.to_le_bytes()).unwrap(); |
| 25 | +} |
| 26 | + |
| 27 | +#[test] |
| 28 | +fn load_consumer_offsets_valid_files() { |
| 29 | + let dir = tempfile::tempdir().unwrap(); |
| 30 | + write_offset_file(dir.path(), "1", 100); |
| 31 | + write_offset_file(dir.path(), "2", 200); |
| 32 | + write_offset_file(dir.path(), "3", 300); |
| 33 | + |
| 34 | + let offsets = load_consumer_offsets(dir.path().to_str().unwrap()).unwrap(); |
| 35 | + |
| 36 | + assert_eq!(offsets.len(), 3); |
| 37 | + assert_eq!(offsets[0].consumer_id, 1); |
| 38 | + assert_eq!(offsets[0].offset.load(Ordering::Relaxed), 100); |
| 39 | + assert_eq!(offsets[0].kind, ConsumerKind::Consumer); |
| 40 | + assert_eq!(offsets[1].consumer_id, 2); |
| 41 | + assert_eq!(offsets[1].offset.load(Ordering::Relaxed), 200); |
| 42 | + assert_eq!(offsets[2].consumer_id, 3); |
| 43 | + assert_eq!(offsets[2].offset.load(Ordering::Relaxed), 300); |
| 44 | +} |
| 45 | + |
| 46 | +#[test] |
| 47 | +fn load_consumer_offsets_skips_non_numeric_files() { |
| 48 | + let dir = tempfile::tempdir().unwrap(); |
| 49 | + write_offset_file(dir.path(), ".DS_Store", 0); |
| 50 | + write_offset_file(dir.path(), "backup.bak", 0); |
| 51 | + write_offset_file(dir.path(), "1", 42); |
| 52 | + |
| 53 | + let offsets = load_consumer_offsets(dir.path().to_str().unwrap()).unwrap(); |
| 54 | + |
| 55 | + assert_eq!(offsets.len(), 1); |
| 56 | + assert_eq!(offsets[0].consumer_id, 1); |
| 57 | + assert_eq!(offsets[0].offset.load(Ordering::Relaxed), 42); |
| 58 | +} |
| 59 | + |
| 60 | +#[test] |
| 61 | +fn load_consumer_offsets_empty_dir() { |
| 62 | + let dir = tempfile::tempdir().unwrap(); |
| 63 | + |
| 64 | + let offsets = load_consumer_offsets(dir.path().to_str().unwrap()).unwrap(); |
| 65 | + |
| 66 | + assert!(offsets.is_empty()); |
| 67 | +} |
| 68 | + |
| 69 | +#[test] |
| 70 | +fn load_consumer_offsets_skips_directories() { |
| 71 | + let dir = tempfile::tempdir().unwrap(); |
| 72 | + std::fs::create_dir(dir.path().join("123")).unwrap(); |
| 73 | + write_offset_file(dir.path(), "1", 77); |
| 74 | + |
| 75 | + let offsets = load_consumer_offsets(dir.path().to_str().unwrap()).unwrap(); |
| 76 | + |
| 77 | + assert_eq!(offsets.len(), 1); |
| 78 | + assert_eq!(offsets[0].consumer_id, 1); |
| 79 | + assert_eq!(offsets[0].offset.load(Ordering::Relaxed), 77); |
| 80 | +} |
| 81 | + |
| 82 | +#[test] |
| 83 | +fn load_consumer_offsets_nonexistent_dir() { |
| 84 | + let result = load_consumer_offsets("/tmp/nonexistent_iggy_test_dir_12345"); |
| 85 | + |
| 86 | + assert!(result.is_err()); |
| 87 | + assert!(matches!( |
| 88 | + result.unwrap_err(), |
| 89 | + IggyError::CannotReadConsumerOffsets(_) |
| 90 | + )); |
| 91 | +} |
| 92 | + |
| 93 | +#[test] |
| 94 | +fn load_consumer_group_offsets_valid_files() { |
| 95 | + let dir = tempfile::tempdir().unwrap(); |
| 96 | + write_offset_file(dir.path(), "1", 500); |
| 97 | + write_offset_file(dir.path(), "2", 600); |
| 98 | + |
| 99 | + let offsets = load_consumer_group_offsets(dir.path().to_str().unwrap()).unwrap(); |
| 100 | + |
| 101 | + assert_eq!(offsets.len(), 2); |
| 102 | + for (group_id, offset) in &offsets { |
| 103 | + assert_eq!(offset.kind, ConsumerKind::ConsumerGroup); |
| 104 | + assert_eq!(offset.consumer_id, group_id.0 as u32); |
| 105 | + } |
| 106 | + let ids: Vec<u32> = offsets.iter().map(|(_, co)| co.consumer_id).collect(); |
| 107 | + assert!(ids.contains(&1)); |
| 108 | + assert!(ids.contains(&2)); |
| 109 | +} |
| 110 | + |
| 111 | +#[test] |
| 112 | +fn load_consumer_group_offsets_skips_non_numeric_files() { |
| 113 | + let dir = tempfile::tempdir().unwrap(); |
| 114 | + write_offset_file(dir.path(), ".DS_Store", 0); |
| 115 | + write_offset_file(dir.path(), "notes.txt", 0); |
| 116 | + write_offset_file(dir.path(), "5", 999); |
| 117 | + |
| 118 | + let offsets = load_consumer_group_offsets(dir.path().to_str().unwrap()).unwrap(); |
| 119 | + |
| 120 | + assert_eq!(offsets.len(), 1); |
| 121 | + assert_eq!(offsets[0].0.0, 5); |
| 122 | + assert_eq!(offsets[0].1.consumer_id, 5); |
| 123 | + assert_eq!(offsets[0].1.offset.load(Ordering::Relaxed), 999); |
| 124 | +} |
| 125 | + |
| 126 | +#[test] |
| 127 | +fn load_consumer_group_offsets_empty_dir() { |
| 128 | + let dir = tempfile::tempdir().unwrap(); |
| 129 | + |
| 130 | + let offsets = load_consumer_group_offsets(dir.path().to_str().unwrap()).unwrap(); |
| 131 | + |
| 132 | + assert!(offsets.is_empty()); |
| 133 | +} |
| 134 | + |
| 135 | +#[test] |
| 136 | +fn load_consumer_group_offsets_nonexistent_dir() { |
| 137 | + let result = load_consumer_group_offsets("/tmp/nonexistent_iggy_test_dir_12345"); |
| 138 | + |
| 139 | + assert!(result.is_err()); |
| 140 | + assert!(matches!( |
| 141 | + result.unwrap_err(), |
| 142 | + IggyError::CannotReadConsumerOffsets(_) |
| 143 | + )); |
| 144 | +} |
0 commit comments