2012-09-24 17 views
9

हेरोकू विभिन्न कारणों से आपके आवेदन में एक सिगरेट भेज सकता है, इसलिए यदि ऐसा होता है तो मैंने कुछ सफाई की देखभाल करने के लिए एक हैंडलर बनाया है। कुछ गूगलिंग ने आरएसपीईसी में इसका परीक्षण करने के तरीके पर कोई जवाब या उदाहरण नहीं दिए हैं।आरएसपीईसी में सिग्नल हैंडलिंग का परीक्षण कैसे करें, विशेष रूप से SIGTERM को संभालना?

Signal.trap('TERM') do 
    cleanup 
end 

def cleanup 
    puts "doing some cleanup stuff" 
    ... 
    exit 
end 

परीक्षण करने के लिए है कि इस सफाई विधि जब कार्यक्रम एक SIGTERM प्राप्त करता है कहा जाता है सबसे अच्छा तरीका क्या है: यहाँ बुनियादी कोड है?

उत्तर

3

अपने आप को मार डालो! Process.kill 'TERM', 0 के साथ आरएसपीसी को सिग्नल भेजें और परीक्षण करें कि हैंडलर को बुलाया जाता है। यह सच है कि यदि सिग्नल फंस नहीं गया है तो परीक्षण विफलता की अच्छी तरह से रिपोर्ट करने के बजाय क्रैश हो जाएगा, लेकिन कम से कम आपको पता चलेगा कि आपके कोड में कोई समस्या है।

उदाहरण के लिए:

class SignalHandler 
    def self.trap_signals 
    Signal.trap('TERM') { term_handler } 
    end 

    def self.term_handler 
    # ... 
    end 

end 

describe SignalHandler do 
    describe '#trap_signals' do 
    it "traps TERM" do 
     # The MRI default TERM handler does not cause RSpec to exit with an error. 
     # Use the system default TERM handler instead, which does kill RSpec. 
     # If you test a different signal you might not need to do this, 
     # or you might need to install a different signal's handler. 
     old_signal_handler = Signal.trap 'TERM', 'SYSTEM_DEFAULT' 

     SignalHandler.trap_signals 
     expect(SignalHandler).to receive(:term_handler).with no_args 
     Process.kill 'TERM', 0 # Send the signal to ourself 

     # Put the Ruby default signal handler back in case it matters to other tests 
     Signal.trap 'TERM', old_signal_handler 
    end 
    end 
end 

मैं केवल परीक्षण किया है कि हैंडलर बुलाया गया था, लेकिन आप समान रूप से अच्छी तरह से हैंडलर का एक पक्ष प्रभाव का परीक्षण कर सकता है।