@@ -76,78 +76,122 @@ public void Verify_that_kpar_contents_can_be_read_from_path()
7676 }
7777
7878 [ Test ]
79- public void Verify_that_kpar_contents_can_be_read_from_stream_leaveOpen_false ( )
79+ public void Verify_that_kpar_contents_can_be_read_from_stream ( )
8080 {
8181 var kparPath = GetKparPath ( ) ;
8282 Assert . That ( File . Exists ( kparPath ) , Is . True , $ "KPAR test file not found: { kparPath } ") ;
8383
84- using var stream = File . OpenRead ( kparPath ) ;
84+ using var fileStream = File . OpenRead ( kparPath ) ;
8585
86- var archive = this . reader . Read ( stream , leaveOpen : false ) ;
86+ var archive = this . reader . Read ( fileStream ) ;
8787
8888 AssertArchive ( archive , expectedPath : null ) ;
89-
90- Assert . That ( stream . CanRead , Is . False , "Stream should be disposed when leaveOpen=false." ) ;
9189 }
9290
9391 [ Test ]
94- public void Verify_that_kpar_contents_can_be_read_from_stream_leaveOpen_true ( )
92+ public async Task Verify_that_kpar_contents_can_be_read_async_from_path ( )
9593 {
9694 var kparPath = GetKparPath ( ) ;
9795 Assert . That ( File . Exists ( kparPath ) , Is . True , $ "KPAR test file not found: { kparPath } ") ;
9896
99- using var stream = File . OpenRead ( kparPath ) ;
97+ var archive = await this . reader . ReadAsync ( kparPath ) . ConfigureAwait ( false ) ;
10098
101- var archive = this . reader . Read ( stream , leaveOpen : true ) ;
99+ AssertArchive ( archive , expectedPath : kparPath ) ;
100+ }
102101
103- AssertArchive ( archive , expectedPath : null ) ;
102+ [ Test ]
103+ public async Task Verify_that_kpar_contents_can_be_read_async_from_stream ( )
104+ {
105+ var kparPath = GetKparPath ( ) ;
106+ Assert . That ( File . Exists ( kparPath ) , Is . True , $ "KPAR test file not found: { kparPath } ") ;
107+
108+ await using var fileStream = File . OpenRead ( kparPath ) ;
104109
105- Assert . That ( stream . CanRead , Is . True , "Stream should remain open when leaveOpen=true." ) ;
110+ var archive = await this . reader . ReadAsync ( fileStream ) . ConfigureAwait ( false ) ;
106111
107- // extra sanity: position should be advanced, but stream still usable
108- Assert . That ( stream . Position , Is . GreaterThan ( 0 ) ) ;
112+ AssertArchive ( archive , expectedPath : null ) ;
109113 }
110114
111115 [ Test ]
112- public async Task Verify_that_kpar_contents_can_be_read_async_from_path ( )
116+ public void Verify_that_kpar_can_be_opened_from_path_and_model_streams_can_be_opened ( )
113117 {
114118 var kparPath = GetKparPath ( ) ;
115119 Assert . That ( File . Exists ( kparPath ) , Is . True , $ "KPAR test file not found: { kparPath } ") ;
116120
117- var archive = await this . reader . ReadAsync ( kparPath ) . ConfigureAwait ( false ) ;
121+ using var archiveSession = this . reader . Open ( kparPath ) ;
118122
119- AssertArchive ( archive , expectedPath : kparPath ) ;
123+ AssertArchive ( archiveSession . Archive , expectedPath : kparPath ) ;
124+
125+ using var modelStream = archiveSession . OpenModel ( "Base" ) ;
126+ Assert . That ( modelStream , Is . Not . Null ) ;
127+ Assert . That ( modelStream . CanRead , Is . True ) ;
128+
129+ Assert . That ( modelStream . Length , Is . GreaterThan ( 0 ) ) ;
120130 }
131+
132+ [ Test ]
133+ public async Task Verify_that_kpar_can_be_opened_async_from_path_and_model_streams_can_be_opened ( )
134+ {
135+ var kparPath = GetKparPath ( ) ;
136+
137+ Assert . That ( File . Exists ( kparPath ) , Is . True , $ "KPAR test file not found: { kparPath } ") ;
121138
139+ await using var archiveSession = await this . reader . OpenAsync ( kparPath ) . ConfigureAwait ( false ) ;
140+
141+ AssertArchive ( archiveSession . Archive , expectedPath : kparPath ) ;
142+
143+ await using var modelStream = archiveSession . OpenModel ( "Base" ) ;
144+ Assert . That ( modelStream , Is . Not . Null ) ;
145+ Assert . That ( modelStream . CanRead , Is . True ) ;
146+ Assert . That ( modelStream . Length , Is . GreaterThan ( 0 ) ) ;
147+ }
148+
122149 [ Test ]
123- public async Task Verify_that_kpar_contents_can_be_read_async_from_stream_leaveOpen_false ( )
150+ public void Verify_that_opened_entry_streams_become_invalid_after_session_dispose ( )
124151 {
125152 var kparPath = GetKparPath ( ) ;
126153 Assert . That ( File . Exists ( kparPath ) , Is . True , $ "KPAR test file not found: { kparPath } ") ;
127154
128- await using var stream = File . OpenRead ( kparPath ) ;
155+ var archiveSession = this . reader . Open ( kparPath ) ;
129156
130- var archive = await this . reader . ReadAsync ( stream , leaveOpen : false ) . ConfigureAwait ( false ) ;
157+ var entryStream = archiveSession . OpenModel ( "Base" ) ;
158+ Assert . That ( entryStream . CanRead , Is . True ) ;
131159
132- AssertArchive ( archive , expectedPath : null ) ;
160+ archiveSession . Dispose ( ) ;
133161
134- Assert . That ( stream . CanRead , Is . False , "Stream should be disposed when leaveOpen=false." ) ;
162+ Assert . That ( ( ) => _ = entryStream . ReadByte ( ) , Throws . Exception ,
163+ "Reading from an entry stream after session disposal should fail." ) ;
135164 }
136165
137166 [ Test ]
138- public async Task Verify_that_kpar_contents_can_be_read_async_from_stream_leaveOpen_true ( )
167+ public async Task Verify_that_kpar_can_be_opened_async_from_stream_and_source_is_disposed_on_session_dispose ( )
139168 {
140169 var kparPath = GetKparPath ( ) ;
141170 Assert . That ( File . Exists ( kparPath ) , Is . True , $ "KPAR test file not found: { kparPath } ") ;
142171
143- await using var stream = File . OpenRead ( kparPath ) ;
172+ var source = File . OpenRead ( kparPath ) ;
144173
145- var archive = await this . reader . ReadAsync ( stream , leaveOpen : true ) . ConfigureAwait ( false ) ;
174+ ArchiveSession archiveSession = null ;
146175
147- AssertArchive ( archive , expectedPath : null ) ;
176+ try
177+ {
178+ archiveSession = await this . reader . OpenAsync ( source ) . ConfigureAwait ( false ) ;
179+
180+ AssertArchive ( archiveSession . Archive , expectedPath : null ) ;
181+
182+ await using var modelStream = archiveSession . OpenModel ( "Base" ) ;
183+ Assert . That ( modelStream . CanRead , Is . True ) ;
184+ Assert . That ( modelStream . Length , Is . GreaterThan ( 0 ) ) ;
185+ }
186+ finally
187+ {
188+ if ( archiveSession is not null )
189+ {
190+ await archiveSession . DisposeAsync ( ) . ConfigureAwait ( false ) ;
191+ }
192+ }
148193
149- Assert . That ( stream . CanRead , Is . True , "Stream should remain open when leaveOpen=true." ) ;
150- Assert . That ( stream . Position , Is . GreaterThan ( 0 ) ) ;
194+ Assert . That ( source . CanRead , Is . False , "Source stream should be disposed ." ) ;
151195 }
152196
153197 /// <summary>
0 commit comments