Skip to content

Instantly share code, notes, and snippets.

@miry
Created March 14, 2022 11:11
Show Gist options
  • Select an option

  • Save miry/d4475803080fc18a5cd5c5a405524fbb to your computer and use it in GitHub Desktop.

Select an option

Save miry/d4475803080fc18a5cd5c5a405524fbb to your computer and use it in GitHub Desktop.

Revisions

  1. miry created this gist Mar 14, 2022.
    62 changes: 62 additions & 0 deletions client.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    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
    47 changes: 47 additions & 0 deletions docker-compose.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    # 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: {}