<commit_before>require 'xmlrpc/httpserver'
require 'json'
require 'thread'
require 'forwardable'

module MockGCM
  class Server
    extend Forwardable

    DEFAULT_HOST = 'localhost'

    def initialize(api_key, port)
      @api_key = api_key

      @received_messages = []
      @canonicals        = {}
      @errors            = {}
      @mutex = Mutex.new

      @server = HttpServer.new(self, port, DEFAULT_HOST, 1, File.open("/dev/null"), false, false)

      # Configurable error behaviour
      @fail_next_request = nil
    end

    # Manage server state

    def_delegators :@server, :start, :stop, :stopped?

    def mock_next_request_failure(errno)
      @mutex.synchronize { @fail_next_request = Integer(errno) }
    end

    def mock_canonical_id(reg_id, canonical_reg_id)
      @mutex.synchronize { @canonicals[reg_id] = canonical_reg_id }
    end

    def mock_error(reg_id, error, options = {})
      @mutex.synchronize { @errors[reg_id] = { :error => error, :times => options[:times] || -1 } }
    end

    # Check server state from request thread

    def error_for(reg_id)
      @mutex.synchronize {
        return unless hsh = @errors[reg_id]
        return unless hsh[:times] != 0<commit_msg>Put variable initiziation under appropriate comment<commit_after>require 'xmlrpc/httpserver'
require 'json'
require 'thread'
require 'forwardable'

module MockGCM
  class Server
    extend Forwardable

    DEFAULT_HOST = 'localhost'

    def initialize(api_key, port)
      @api_key = api_key

      @received_messages = []
      @mutex = Mutex.new

      @server = HttpServer.new(self, port, DEFAULT_HOST, 1, File.open("/dev/null"), false, false)

      # Configurable error behaviour
      @fail_next_request = nil
      @canonicals        = {}
      @errors            = {}
    end

    # Manage server state

    def_delegators :@server, :start, :stop, :stopped?

    def mock_next_request_failure(errno)
      @mutex.synchronize { @fail_next_request = Integer(errno) }
    end

    def mock_canonical_id(reg_id, canonical_reg_id)
      @mutex.synchronize { @canonicals[reg_id] = canonical_reg_id }
    end

    def mock_error(reg_id, error, options = {})
      @mutex.synchronize { @errors[reg_id] = { :error => error, :times => options[:times] || -1 } }
    end

    # Check server state from request thread

    def error_for(reg_id)
      @mutex.synchronize {
        return unless hsh = @errors[reg_id]
        return unless hsh[:times] != 0
{'fraction_non_alphanumeric': 0.10722989439480098, 'fraction_numerical': 0.002437043054427295, 'mean_word_length': 2.9345047923322682, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3790375', 'n_tokens_mistral': 804, 'n_tokens_neox': 769, 'n_words': 184}