Skip to content

Commit

Permalink
more specs
Browse files Browse the repository at this point in the history
  • Loading branch information
mensfeld committed Aug 1, 2023
1 parent 5da2b24 commit aa3f011
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/karafka/web/ui/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ def per_page
# @param start_offset [Integer] offset of the first message (lowest) that we received
# @param count [Integer] how many messages we wanted - we need that to fill spots to
# have exactly the number that was requested and not more
# @param high_offset [Integer] high watermark offset
# @return [Array<Karafka::Messages::Message, Integer>] array with gaps filled with the
# missing offset
def fill_compacted(messages, partition_id, start_offset, count, high_offset)
Expand Down
73 changes: 73 additions & 0 deletions spec/lib/karafka/web/ui/models/message_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# frozen_string_literal: true

require 'karafka/pro/iterator'
require 'karafka/pro/iterator/expander'
require 'karafka/pro/iterator/tpl_builder'

RSpec.describe_current do
let(:topic) { create_topic }
let(:partition) { 0 }
Expand Down Expand Up @@ -190,9 +194,78 @@
expect(page[2]).to eq(25)
end
end

context 'when there is a lot of data and we want the most recent page (-1)' do
before { produce_many(topic, (0..99).map(&:to_s)) }

let(:start_offset) { -1 }

it 'expect to return it in order' do
expect(page[0]).to eq(false)
expect(page[1].map(&:offset)).to eq((75..99).to_a.reverse)
expect(page[2]).to eq(50)
end
end
end

describe '#topic_page' do
subject(:result) { described_class.topic_page(topic, partitions_ids, page) }

let(:topic) { create_topic(partitions: 4) }
let(:partitions_ids) { (0..3).to_a }
let(:page) { 1 }

context 'when there is no data in any of the partitions' do
it { expect(result).to eq([[], false]) }
end

context 'when we try to reach beyond first page on empty' do
let(:page) { 2 }

it { expect(result).to eq([[], false]) }
end

context 'when there is some data in the first partition only' do
before { produce_many(topic, (0..3).map(&:to_s), partition: 0) }

it 'expect to return this data' do
expect(result[0].map(&:offset)).to eq([3, 2, 1, 0])
expect(result[1]).to eq(false)
end
end

context 'when there is some data in each of the partitions' do
before do
produce(topic, '0', partition: 0)
produce(topic, '1', partition: 1)
produce(topic, '2', partition: 2)
produce(topic, '3', partition: 3)
end

it 'expect to return this data' do
expect(result[0].map(&:offset)).to eq([0, 0, 0, 0])
expect(result[0].map(&:partition)).to eq([0, 1, 2, 3])
expect(result[1]).to eq(false)
end
end

context 'when there are more partitions than space on the first page but data only beyond' do
let(:topic) { create_topic(partitions: 100) }
let(:partitions_ids) { (25..29).to_a }

before do
produce(topic, '0', partition: 24)
produce(topic, '0', partition: 25)
produce(topic, '1', partition: 26)
produce(topic, '2', partition: 27)
produce(topic, '3', partition: 28)
end

it 'expect to fetch from partitions we want' do
expect(result[0].map(&:offset)).to eq([0, 0, 0, 0])
expect(result[0].map(&:partition)).to eq((25..28).to_a)
expect(result[1]).to eq(false)
end
end
end
end
19 changes: 19 additions & 0 deletions spec/lib/karafka/web/ui/models/partition_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

RSpec.describe_current do
subject(:partition) { described_class.new(data) }

let(:data) { {} }

describe '#lag' do
context 'when not available' do
it { expect(partition.lag).to eq(-1) }
end

context 'when available' do
let(:data) { { lag: 100 } }

it { expect(partition.lag).to eq(100) }
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class App
# @param topic_name [String] topic name. Default will generate automatically
# @param partitions [Integer] number of partitions (one by default)
# @return [String] generated topic name
def create_topic(topic_name = SecureRandom.uuid, partitions = 1)
def create_topic(topic_name: SecureRandom.uuid, partitions: 1)
Karafka::Admin.create_topic(topic_name, partitions, 1)
topic_name
end
Expand Down

0 comments on commit aa3f011

Please sign in to comment.