File: memcached_mock.rb

package info (click to toggle)
ruby-dalli 3.2.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 684 kB
  • sloc: ruby: 6,552; sh: 20; makefile: 4
file content (37 lines) | stat: -rw-r--r-- 887 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true

require 'tempfile'

##
# Utility for generating a mocked memcached instance.  The mocked instance
# takes a block, which defines the behavior of the socket.  Both TCP and
# UNIX domain sockets are supported.
##
module MemcachedMock
  UNIX_SOCKET_PATH = (f = Tempfile.new('dalli_test')
                      f.close
                      f.path)

  def self.start(port = 19_123)
    server = TCPServer.new('localhost', port)
    session = server.accept
    yield(session)
  end

  def self.start_unix(path = UNIX_SOCKET_PATH)
    begin
      File.delete(path)
    rescue Errno::ENOENT
      # Ignore file not found errors
    end
    server = UNIXServer.new(path)
    session = server.accept
    yield(session)
  end

  def self.delayed_start(port = 19_123, wait = 1)
    server = TCPServer.new('localhost', port)
    sleep wait
    yield(server)
  end
end