Skip to content

Commit

Permalink
filter out unsupported policies (#600)
Browse files Browse the repository at this point in the history
* filter out unsupported policies

* add spec
  • Loading branch information
kickster97 authored Nov 24, 2023
1 parent dacac51 commit f132fb9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
17 changes: 17 additions & 0 deletions spec/policies_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ describe LavinMQ::VHost do
definitions = {
"max-length" => JSON::Any.new(10_i64),
"alternate-exchange" => JSON::Any.new("dead-letters"),
"unsupported" => JSON::Any.new("unsupported"),
}
vhost = Server.vhosts.create("add_policy")

Expand Down Expand Up @@ -212,6 +213,22 @@ describe LavinMQ::VHost do
ch.queue_declare(q.name, passive: true)[:message_count].should eq 2
end
end

it "effective_policy_definition should not include unsupported policies" do
supported_policies = {"max-length", "max-length-bytes",
"message-ttl", "expires", "overflow",
"dead-letter-exchange", "dead-letter-routing-key",
"federation-upstream", "federation-upstream-set",
"delivery-limit", "max-age", "alternate-exchange",
"delayed-message"}
vhost.queues["test"] = LavinMQ::Queue.new(vhost, "test")
vhost.add_policy("test", "^.*$", "all", definitions, -10_i8)
sleep 0.01
vhost.queues["test"].details_tuple[:effective_policy_definition].as(Hash(String, JSON::Any)).each do |k, v|
supported_policies.includes?(k).should be_true
end
vhost.delete_policy("test")
end
end

describe "together with arguments" do
Expand Down
10 changes: 7 additions & 3 deletions src/lavinmq/policy.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module LavinMQ
end

class Policy
SUPPORTED_POLICIES = {"max-length", "max-length-bytes", "message-ttl", "expires", "overflow",
"dead-letter-exchange", "dead-letter-routing-key", "federation-upstream",
"federation-upstream-set", "delivery-limit", "max-age",
"alternate-exchange", "delayed-message"}
enum Target
All
Queues
Expand Down Expand Up @@ -67,15 +71,15 @@ module LavinMQ
merged[k] = v
end
end
merged
merged.select { |key, _| SUPPORTED_POLICIES.includes?(key) }
end

def self.merge_definitions(p1 : Nil, p2 : Policy) : Hash(String, JSON::Any)
p2.definition
p2.definition.select { |key, _| SUPPORTED_POLICIES.includes?(key) }
end

def self.merge_definitions(p1 : Policy, p2 : Nil) : Hash(String, JSON::Any)
p1.definition
p1.definition.select { |key, _| SUPPORTED_POLICIES.includes?(key) }
end

def self.merge_definitions(p1 : Nil, p2 : Nil) : Hash(String, JSON::Any)
Expand Down

0 comments on commit f132fb9

Please sign in to comment.