Path: blob/trunk/rb/spec/unit/selenium/webdriver/socket_poller_spec.rb
1864 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819require_relative 'spec_helper'2021module Selenium22module WebDriver23describe SocketPoller do24before(:context) do25@server_thread = Thread.new do26server = TCPServer.open(Platform.localhost, 9250)27Thread.current.thread_variable_set(:server, server)28loop { server.accept.close }29end30@server_thread.report_on_exception = false31end3233after(:context) do34@server_thread.thread_variable_get(:server).close35end3637def poller(port)38SocketPoller.new('localhost', port, 5, 0.05)39end4041describe '#connected?' do42it 'returns true when the socket is listening' do43expect(poller(9250)).to be_connected44end4546it 'returns false if the socket is not listening after the given timeout' do47start = Time.parse('2010-01-01 00:00:00')48wait = Time.parse('2010-01-01 00:00:04')49stop = Time.parse('2010-01-01 00:00:06')5051allow(Process).to receive(:clock_gettime).and_return(start, wait, stop)52expect(poller(9251)).not_to be_connected53end54end5556describe '#closed?' do57it 'returns true when the socket is closed' do58expect(poller(9251)).to be_closed59end6061it 'returns false if the socket is still listening after the given timeout' do62start = Time.parse('2010-01-01 00:00:00').to_f63wait = Time.parse('2010-01-01 00:00:04').to_f64stop = Time.parse('2010-01-01 00:00:06').to_f6566allow(Process).to receive(:clock_gettime).and_return(start, wait, stop)67expect(poller(9250)).not_to be_closed68end69end70end71end # WebDriver72end # Selenium737475