spec/spec_helper.rb in deimos-ruby-1.24.2 vs spec/spec_helper.rb in deimos-ruby-2.0.0.pre.alpha1

- old
+ new

@@ -12,22 +12,47 @@ require 'activerecord-import' require 'handlers/my_batch_consumer' require 'handlers/my_consumer' require 'rspec/rails' require 'rspec/snapshot' +require 'karafka/testing/rspec/helpers' require "trilogy_adapter/connection" ActiveRecord::Base.public_send :extend, TrilogyAdapter::Connection Dir['./spec/schemas/**/*.rb'].sort.each { |f| require f } # Constants used for consumer specs SCHEMA_CLASS_SETTINGS = { off: false, on: true }.freeze class DeimosApp < Rails::Application end +DeimosApp.initializer("setup_root_dir", before: "karafka.require_karafka_boot_file") do + ENV['KARAFKA_ROOT_DIR'] = "#{Rails.root}/spec/karafka" +end DeimosApp.initialize! -# Helpers for Executor/DbProducer +module Helpers + + def set_karafka_config(method, val) + Deimos.karafka_configs.each { |c| c.send(method.to_sym, val) } + end + + def register_consumer(klass, schema, namespace='com.my-namespace', key_config:{none: true}, configs: {}) + Karafka::App.routes.redraw do + topic 'my-topic' do + consumer klass + schema schema + namespace namespace + key_config key_config + configs.each do |k, v| + public_send(k, v) + end + end + end + end +end + +# Helpers for Executor/OutboxProducer module TestRunners # Execute a block until it stops failing. This is helpful for testing threads # where we need to wait for them to continue but don't want to rely on # sleeping for X seconds, which is crazy brittle and slow. def wait_for @@ -71,28 +96,24 @@ module DbConfigs # @param payload [Hash] # @param topic [String] # @param key [String] def build_message(payload, topic, key) - message = Deimos::Message.new(payload, Deimos::Producer, - topic: topic, key: key) - message.encoded_payload = message.payload - message.encoded_key = message.key - message + { payload: payload, topic: topic, key: key} end DB_OPTIONS = [ { adapter: 'postgresql', port: 5432, username: 'postgres', - password: 'root', + password: 'password', database: 'postgres', host: ENV['PG_HOST'] || 'localhost' }, { - adapter: 'mysql2', + adapter: 'trilogy', port: 3306, username: 'root', database: 'test', host: ENV['MYSQL_HOST'] || '127.0.0.1' }, @@ -121,18 +142,18 @@ end end end # :nodoc: - def run_db_backend_migration - migration_class_name = 'DbBackendMigration' + def run_outbox_backend_migration + migration_class_name = 'OutboxBackendMigration' migration_version = '[5.2]' migration = ERB.new( - File.read('lib/generators/deimos/db_backend/templates/migration') + File.read('lib/generators/deimos/outbox_backend/templates/migration') ).result(binding) eval(migration) # rubocop:disable Security/Eval - ActiveRecord::Migration.new.run(DbBackendMigration, direction: :up) + ActiveRecord::Migration.new.run(OutboxBackendMigration, direction: :up) end # :nodoc: def run_db_poller_migration migration_class_name = 'DbPollerMigration' @@ -145,11 +166,11 @@ end # Set up the given database. def setup_db(options) ActiveRecord::Base.establish_connection(options) - run_db_backend_migration + run_outbox_backend_migration run_db_poller_migration ActiveRecord::Base.descendants.each do |klass| klass.reset_sequence_name if klass.respond_to?(:reset_sequence_name) # reset internal variables - terrible hack to trick Rails into doing this @@ -161,11 +182,14 @@ end RSpec.configure do |config| config.extend(DbConfigs) include DbConfigs + config.include Karafka::Testing::RSpec::Helpers + config.include TestRunners + config.include Helpers config.full_backtrace = true config.snapshot_dir = "spec/snapshots" # true by default for RSpec 4.0 @@ -197,24 +221,26 @@ end config.before(:each) do Deimos.config.reset! Deimos.configure do |deimos_config| - deimos_config.producers.backend = :test + deimos_config.producers.backend = :kafka deimos_config.schema.nest_child_schemas = true - deimos_config.phobos_config_file = File.join(File.dirname(__FILE__), 'phobos.yml') deimos_config.schema.path = File.join(File.expand_path(__dir__), 'schemas') deimos_config.consumers.reraise_errors = true deimos_config.schema.registry_url = ENV['SCHEMA_REGISTRY'] || 'http://localhost:8081' - deimos_config.kafka.seed_brokers = ENV['KAFKA_SEED_BROKER'] || 'localhost:9092' deimos_config.logger = Logger.new('/dev/null') deimos_config.logger.level = Logger::INFO deimos_config.schema.backend = :avro_validation deimos_config.schema.generated_class_path = 'spec/schemas' end end + config.after(:each) do + Deimos::EVENT_TYPES.each { |type| Karafka.monitor.notifications_bus.clear(type) } + end + config.around(:each) do |example| use_cleaner = !example.metadata[:integration] DatabaseCleaner.start if use_cleaner @@ -260,28 +286,34 @@ end end RSpec.shared_context('with publish_backend') do before(:each) do - producer_class = Class.new(Deimos::Producer) do - schema 'MySchema' - namespace 'com.my-namespace' - topic 'my-topic' - key_config field: 'test_id' - end + producer_class = Class.new(Deimos::Producer) stub_const('MyProducer', producer_class) - producer_class = Class.new(Deimos::Producer) do - schema 'MySchema' - namespace 'com.my-namespace' - topic 'my-topic' - key_config none: true - end + producer_class_no_key = Class.new(Deimos::Producer) stub_const('MyNoKeyProducer', producer_class) + + Karafka::App.routes.redraw do + topic 'my-topic-no-key' do + schema 'MySchema' + namespace 'com.my-namespace' + key_config none: true + producer_class producer_class_no_key + end + topic 'my-topic' do + schema 'MySchema' + namespace 'com.my-namespace' + key_config field: 'test_id' + producer_class producer_class + end + end + end let(:messages) do (1..3).map do |i| - build_message({ foo: i }, 'my-topic', "foo#{i}") + build_message({ test_id: "foo#{i}", some_int: i }, 'my-topic', "foo#{i}") end end end