diff options
author | Adam Edwards <adamed@opscode.com> | 2014-05-15 14:14:01 -0700 |
---|---|---|
committer | Adam Edwards <adamed@opscode.com> | 2014-05-15 14:14:01 -0700 |
commit | 1cf40fe00e65ac2e6911bce64154e5111687b90d (patch) | |
tree | 65b6391650c78d49a35b7bacd5bc5552fb971586 | |
parent | d9de698a09a1ee4395bd06225e110e966d150e51 (diff) | |
download | wmi-lite-1cf40fe00e65ac2e6911bce64154e5111687b90d.tar.gz |
Delay connection to first query, not constructor
-rw-r--r-- | lib/wmi-lite/wmi.rb | 13 | ||||
-rw-r--r-- | spec/functional/wmi_spec.rb | 3 | ||||
-rw-r--r-- | spec/unit/wmi_spec.rb | 22 |
3 files changed, 33 insertions, 5 deletions
diff --git a/lib/wmi-lite/wmi.rb b/lib/wmi-lite/wmi.rb index 1acc9bd..cc6492d 100644 --- a/lib/wmi-lite/wmi.rb +++ b/lib/wmi-lite/wmi.rb @@ -22,7 +22,8 @@ require 'wmi-lite/wmi_instance' module WmiLite class Wmi def initialize(namespace = nil) - @connection = new_connection(namespace.nil? ? 'root/cimv2' : namespace) + @namespace = namespace + @connection = nil end def query(wql_query) @@ -54,6 +55,7 @@ module WmiLite private def start_query(wql_query) + connect_to_namespace result = @connection.ExecQuery(wql_query) raise_if_failed(result) result @@ -66,9 +68,12 @@ module WmiLite result.count end - def new_connection(namespace) - locator = WIN32OLE.new("WbemScripting.SWbemLocator") - locator.ConnectServer('.', namespace) + def connect_to_namespace + if @connection.nil? + namespace = @namespace.nil? ? 'root/cimv2' : @namespace + locator = WIN32OLE.new("WbemScripting.SWbemLocator") + @connection = locator.ConnectServer('.', namespace) + end end def wmi_result_to_snapshot(wmi_object) diff --git a/spec/functional/wmi_spec.rb b/spec/functional/wmi_spec.rb index badca78..53bf548 100644 --- a/spec/functional/wmi_spec.rb +++ b/spec/functional/wmi_spec.rb @@ -39,7 +39,8 @@ describe WmiLite::Wmi, :windows_only do shared_examples_for 'an invalid query' do it 'should raise an exception if an invalid namespace is specified' do - expect { invalid_wmi = WmiLite::Wmi.new('root/notvalid') }.to raise_error + invalid_wmi = WmiLite::Wmi.new('root/notvalid') + expect { invalid_wmi.ExecQuery('Win32_Process') }.to raise_error end it 'should raise an exception if an invalid class is specified' do diff --git a/spec/unit/wmi_spec.rb b/spec/unit/wmi_spec.rb index a6fb0b5..5aac6b9 100644 --- a/spec/unit/wmi_spec.rb +++ b/spec/unit/wmi_spec.rb @@ -199,6 +199,28 @@ describe WmiLite::Wmi do end + context "when constructing a Ruby class instance" do + it "should not connect to WMI in the constructor" do + WmiLite::Wmi.any_instance.should_not_receive(:connect_to_namespace) + wmi_service_nil_namespace = WmiLite::Wmi.new + wmi_service_explicit_namespace = WmiLite::Wmi.new('root/cimv2') + end + end + + context "when calling query methods" do + it "should only connect to WMI on the first query execution" do + WIN32OLE.should_receive(:new).with("WbemScripting.SWbemLocator").exactly(1).times.and_return(wbem_locator) + wmi_service = WmiLite::Wmi.new + + # Make a lot of queries to be sure the connection is only created once + wmi_service.query('select * from Win32_Process') + wmi_service.query('select * from Win32_Process') + wmi_service.instances_of('Win32_Processor') + wmi_service.instances_of('Win32_Processor') + wmi_service.first_of('Win32_Group') + wmi_service.first_of('Win32_Group') + end + end it_should_behave_like "the first_of method" |