@@ -113,10 +113,53 @@ public async Task LoadAsyncProcessSuccess (bool extraData)
113
113
Assert . AreEqual ( 0 , devices . ConnectedTV . Count ( ) ) ;
114
114
}
115
115
116
- private void AssertArgumentValue ( MlaunchArgument arg , string expected , string message = null )
116
+ [ Test ]
117
+ public async Task FindAndCacheDevicesWithFailingMlaunchTest ( )
117
118
{
118
- var value = arg . AsCommandLineArgument ( ) . Split ( new char [ ] { '=' } , 2 ) . LastOrDefault ( ) ;
119
- Assert . AreEqual ( expected , value , message ) ;
119
+ string processPath = null ;
120
+ MlaunchArguments passedArguments = null ;
121
+
122
+ // Moq.SetupSequence doesn't allow custom callbacks so we need to count ourselves
123
+ var calls = 0 ;
124
+
125
+ // moq It.Is is not working as nicelly as we would like it, we capture data and use asserts
126
+ processManager . Setup ( p => p . RunAsync ( It . IsAny < Process > ( ) , It . IsAny < MlaunchArguments > ( ) , It . IsAny < ILog > ( ) , It . IsAny < TimeSpan ? > ( ) , It . IsAny < Dictionary < string , string > > ( ) , It . IsAny < CancellationToken ? > ( ) , It . IsAny < bool ? > ( ) ) )
127
+ . Returns < Process , MlaunchArguments , ILog , TimeSpan ? , Dictionary < string , string > , CancellationToken ? , bool ? > ( ( p , args , log , t , env , token , d ) => {
128
+ calls ++ ;
129
+
130
+ if ( calls == 1 ) {
131
+ // Mlaunch can sometimes time out and we are testing that a subsequent Load will trigger it again
132
+ return Task . FromResult ( new ProcessExecutionResult { ExitCode = 137 , TimedOut = true } ) ;
133
+ }
134
+
135
+ processPath = p . StartInfo . FileName ;
136
+ passedArguments = args ;
137
+
138
+ // we get the temp file that was passed as the args, and write our sample xml, which will be parsed to get the devices :)
139
+ var tempPath = args . Where ( a => a is ListDevicesArgument ) . First ( ) . AsCommandLineArgument ( ) ;
140
+ tempPath = tempPath . Substring ( tempPath . IndexOf ( '=' ) + 1 ) . Replace ( "\" " , string . Empty ) ;
141
+
142
+ var name = GetType ( ) . Assembly . GetManifestResourceNames ( ) . Where ( a => a . EndsWith ( "devices.xml" , StringComparison . Ordinal ) ) . FirstOrDefault ( ) ;
143
+ using ( var outputStream = new StreamWriter ( tempPath ) )
144
+ using ( var sampleStream = new StreamReader ( GetType ( ) . Assembly . GetManifestResourceStream ( name ) ) ) {
145
+ string line ;
146
+ while ( ( line = sampleStream . ReadLine ( ) ) != null )
147
+ outputStream . WriteLine ( line ) ;
148
+ }
149
+ return Task . FromResult ( new ProcessExecutionResult { ExitCode = 0 , TimedOut = false } ) ;
150
+ } ) ;
151
+
152
+ Assert . ThrowsAsync < Exception > ( async ( ) => await devices . LoadDevices ( executionLog . Object ) ) ;
153
+
154
+ Assert . IsEmpty ( devices . ConnectedDevices ) ;
155
+ Assert . AreEqual ( 1 , calls ) ;
156
+ await devices . LoadDevices ( executionLog . Object ) ;
157
+ Assert . AreEqual ( 2 , calls ) ;
158
+ Assert . IsNotEmpty ( devices . ConnectedDevices ) ;
159
+ await devices . LoadDevices ( executionLog . Object ) ;
160
+ Assert . AreEqual ( 2 , calls ) ;
161
+ await devices . LoadDevices ( executionLog . Object ) ;
162
+ Assert . AreEqual ( 2 , calls ) ;
120
163
}
121
164
}
122
165
}
0 commit comments