@@ -33,9 +33,11 @@ public void Ctor_ExpectedDefaults()
33
33
}
34
34
35
35
[ Theory ]
36
- [ InlineData ( false ) ]
37
- [ InlineData ( true ) ]
38
- public async Task CachesSuccessResultsAsync ( bool conversationIdSet )
36
+ [ InlineData ( false , false ) ]
37
+ [ InlineData ( false , true ) ]
38
+ [ InlineData ( true , false ) ]
39
+ [ InlineData ( true , true ) ]
40
+ public async Task CachesSuccessResultsAsync ( bool conversationIdSet , bool customCaching )
39
41
{
40
42
// Arrange
41
43
ChatOptions options = new ( ) { ConversationId = conversationIdSet ? "123" : null } ;
@@ -79,10 +81,16 @@ public async Task CachesSuccessResultsAsync(bool conversationIdSet)
79
81
return Task . FromResult ( expectedResponse ) ;
80
82
}
81
83
} ;
82
- using var outer = new DistributedCachingChatClient ( testClient , _storage )
83
- {
84
- JsonSerializerOptions = TestJsonSerializerContext . Default . Options
85
- } ;
84
+
85
+ int enableCachingInvocations = 0 ;
86
+ using var outer = customCaching ?
87
+ new CustomCachingChatClient ( testClient , _storage , ( m , o ) =>
88
+ {
89
+ return ++ enableCachingInvocations % 2 == 0 ;
90
+ } ) :
91
+ new DistributedCachingChatClient ( testClient , _storage ) ;
92
+
93
+ outer . JsonSerializerOptions = TestJsonSerializerContext . Default . Options ;
86
94
87
95
// Make the initial request and do a quick sanity check
88
96
var result1 = await outer . GetResponseAsync ( "some input" , options ) ;
@@ -93,12 +101,28 @@ public async Task CachesSuccessResultsAsync(bool conversationIdSet)
93
101
var result2 = await outer . GetResponseAsync ( "some input" , options ) ;
94
102
95
103
// Assert
96
- Assert . Equal ( conversationIdSet ? 2 : 1 , innerCallCount ) ;
104
+ if ( customCaching )
105
+ {
106
+ Assert . Equal ( enableCachingInvocations % 2 == 0 ? 2 : 1 , innerCallCount ) ;
107
+ }
108
+ else
109
+ {
110
+ Assert . Equal ( conversationIdSet ? 2 : 1 , innerCallCount ) ;
111
+ }
112
+
97
113
AssertResponsesEqual ( expectedResponse , result2 ) ;
98
114
99
115
// Act/Assert 2: Cache misses do not return cached results
100
116
await outer . GetResponseAsync ( "some modified input" , options ) ;
101
- Assert . Equal ( conversationIdSet ? 3 : 2 , innerCallCount ) ;
117
+ Assert . Equal ( conversationIdSet || customCaching ? 3 : 2 , innerCallCount ) ;
118
+
119
+ Assert . Equal ( customCaching ? 3 : 0 , enableCachingInvocations ) ;
120
+ }
121
+
122
+ private sealed class CustomCachingChatClient ( IChatClient innerClient , IDistributedCache storage , Func < IEnumerable < ChatMessage > , ChatOptions ? , bool > enableCaching ) :
123
+ DistributedCachingChatClient ( innerClient , storage )
124
+ {
125
+ protected override bool EnableCaching ( IEnumerable < ChatMessage > messages , ChatOptions ? options ) => enableCaching ( messages , options ) ;
102
126
}
103
127
104
128
[ Fact ]
0 commit comments