Sha256: 6e7ce410a8c0b5aa14567462ce8eb0213cfd05fa89eac430f75de207eb7dc923

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

require 'socket'
require 'fileutils'

module I3Ipc
  # Simple socket server that communicates with
  # client simulating i3-ipc messages.
  class I3MockServer
    SOCKET_PATH = '/tmp/i3-mock-server.sock'

    def initialize
      remove_sock
      @server = UNIXServer.new(SOCKET_PATH)
    end

    def accept_client
      @client = @server.accept_nonblock
      self
    rescue IO::WaitReadable, Errno::EINTR
      nil
    end

    def client_alive?
      return false unless @client
      @client.write 'hi'
      true
    rescue Errno::EPIPE
      false
    end

    def receive(len)
      raise 'Client not accepted yet' unless @client
      @client.read(len)
    end

    def send(data)
      raise 'Client not accepted yet' unless @client
      @client.write(data)
    end

    def close_client
      @client.close if @client
      @client = nil
    end

    def close
      close_client
      @server.close unless @server.closed?
      remove_sock
    end

    private

    def remove_sock
      FileUtils.rm(SOCKET_PATH) if File.exist?(SOCKET_PATH)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
i3ipc-0.4.0 spec/i3_mock_server.rb
i3ipc-0.3.0 spec/i3_mock_server.rb
i3ipc-0.2.0 spec/i3_mock_server.rb
i3ipc-0.1.0 spec/i3_mock_server.rb