Created
March 14, 2022 11:11
-
-
Save miry/d4475803080fc18a5cd5c5a405524fbb to your computer and use it in GitHub Desktop.
Testing toxiproxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
require 'timeout' | |
require "toxiproxy" | |
hostname = 'toxiproxy' | |
port = 16379 | |
# Setup | |
Toxiproxy.host = "http://#{hostname}:8474" | |
Toxiproxy.populate([{ | |
name: "redis", | |
listen: ":#{port}", | |
upstream: "redis:6379", | |
}]) | |
Toxiproxy.each do |proxy| | |
p proxy | |
end | |
puts "## Test connect to redis" | |
s = TCPSocket.open(hostname, port) | |
s.puts "info" | |
Timeout::timeout(2) do | |
puts ">>> Start reading" | |
while line = s.gets | |
puts line.chop | |
end | |
rescue => e | |
puts e | |
p e | |
end rescue "" | |
s.close | |
puts ">>> Finish reading" | |
puts "## Test when redis is down" | |
Toxiproxy[/redis/].down do | |
Toxiproxy.each do |proxy| | |
p proxy | |
end | |
s = TCPSocket.open(hostname, port) | |
s.puts "info" | |
Timeout::timeout(2) do | |
puts ">>> Start reading" | |
while line = s.gets | |
puts line.chop | |
end | |
rescue => e | |
puts e | |
p e | |
end rescue "" | |
s.close | |
puts ">>> Finish reading" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# docker-compose.yml | |
version: "3.9" | |
services: | |
redis: | |
image: redis:6.2 | |
ports: | |
- "6379:6379" | |
expose: | |
- "6379" | |
networks: | |
example: | |
aliases: | |
- redis | |
toxiproxy: | |
image: ghcr.io/shopify/toxiproxy:2.4.0 | |
networks: | |
example: | |
aliases: | |
- toxiproxy | |
expose: | |
- "8474" | |
- "16379" | |
ports: | |
- "8474:8474" | |
- "16379:16379" | |
app: | |
image: ruby:3.1 | |
networks: | |
example: | |
aliases: | |
- app | |
depends_on: | |
- redis | |
- toxiproxy | |
volumes: | |
- ./client.rb:/client.rb | |
command: | |
- bash | |
- -c | |
- | | |
gem install toxiproxy | |
ruby client.rb | |
networks: | |
example: {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment