2016-07-29 5 views
5

named pipe을 사용하여 다른 프로세스와 통신하는 Windows 서비스를 구축 중입니다. 명명 된 파이프 통신 장치 내 시험이 오류 메시지가 4 번 던지고 :완료하기 전에 테스트로 시작된 모든 스레드가 중지되었는지 확인하는 방법

System.AppDomainUnloadedException하면 : 무부하 AppDomain에 액세스하려고. 테스트가 스레드를 시작했지만 스레드를 중지하지 않은 경우에 발생할 수 있습니다. 완료되기 전에 테스트 (들)에 의해 시작된 모든 스레드가 중지되었는지 확인하십시오. 어떻게 내 단위 테스트가 정지하기 전에 모든 스레드가 중지해야합니까

[TestMethod] 
    public void ListenToNamedPipeTest() 
    { 
     var watcher = new ManualResetEvent(false); 

     var svc = new WindowService(); 
     svc.ClientMessageHandler += (connection, message) => watcher.Reset(); 
     svc.ListenToNamedPipe(); 
     sendMessageToNamedPipe("bla"); 
     var wait = watcher.WaitOne(1000); 

     svc.Dispose(); 

     Assert.IsTrue(wait, "No messages received after 1 seconds"); 
    } 

    private void sendMessageToNamedPipe(string text) 
    { 
     var client = new NamedPipeClient<Message, Message>(DeviceCertificateService.PIPE_NAME); 
     client.ServerMessage += (conn, message) => Console.WriteLine("Server says: {0}", message.Text); 

     // Start up the client asynchronously and connect to the specified server pipe. 
     // This method will return immediately while the client runs in a separate background thread. 
     client.Start(); 

     client.PushMessage(new Message { Text = text }); 

     client.Stop(); 
    } 

:

여기 내 단위 테스트입니까?

감사


는 UPDATE :

// Type: NamedPipeWrapper.NamedPipeClient`2 
// Assembly: NamedPipeWrapper, Version=1.5.0.0, Culture=neutral, PublicKeyToken=null 
// MVID: D2B99F4D-8C17-4DB6-8A02-29DCF82A4118 
// Assembly location: C:\Users\Thang.Duong\Source\Workspaces\Post Tracking System\Applications\Dev\OHD\packages\NamedPipeWrapper.1.5.0\lib\net40\NamedPipeWrapper.dll 

using System; 

namespace NamedPipeWrapper 
{ 
    public class NamedPipeClient<TRead, TWrite> where TRead : class where TWrite : class 
    { 
    public NamedPipeClient(string pipeName); 
    public void Start(); 
    public void PushMessage(TWrite message); 
    public void Stop(); 
    public void WaitForConnection(); 
    public void WaitForConnection(int millisecondsTimeout); 
    public void WaitForConnection(TimeSpan timeout); 
    public void WaitForDisconnection(); 
    public void WaitForDisconnection(int millisecondsTimeout); 
    public void WaitForDisconnection(TimeSpan timeout); 
    public bool AutoReconnect { get; set; } 
    public event ConnectionMessageEventHandler<TRead, TWrite> ServerMessage; 
    public event ConnectionEventHandler<TRead, TWrite> Disconnected; 
    public event PipeExceptionEventHandler Error; 
    } 
} 
+0

서버와 클라이언트 모두에서 .close와 .dispose가 필요하지 않습니까? – GamerJ5

+0

클라이언트 나 서버 모두'Close()'기능을 가지고 있지 않습니다. 각각은'Stop()'함수를 가지고 있습니다. – Believe2014

+0

ManualResetEvent가 닫습니다. 시도해 보셨습니까? 그것은 당신이 산란 유일한 스레드 인 것 같습니다. – GamerJ5

답변

2

WindowsService 모두 닫습니다 Dispose() 기능이있는 ServiceBase 클래스를 상속 내 :

명명 된 파이프 클라이언트는 close() 기능이 없습니다 스레드. 그것이 내가 모든 경주 오류를 얻는 이유입니다.

나는 Dispose() 함수를 호출하고 client.Close()svc.Close() 함수로 바꾸지 않아야했습니다. svc.Close() 함수는 명명 된 파이프 서버를 중지하고 닫는 사용자 정의 구현입니다.