11const { MemoryCache } = require ( '..' ) ;
2- const { SerializingCache } = require ( '..' ) ;
2+ const { SerializingCache, NonSerializingCache } = require ( '..' ) ;
33const { DelayedCache } = require ( './util/delayed' ) ;
44
55const assert = require ( 'assert' ) ;
@@ -113,3 +113,113 @@ describe("SerializingCache", function () {
113113 } ) ;
114114 } ) ;
115115} ) ;
116+
117+ describe ( "NonSerializingCache" , function ( ) {
118+ describe ( 'get' , function ( ) {
119+ it ( "should fill using callback" , function ( ) {
120+ const testValue = 'value' ;
121+ const testKey = 'key' ;
122+
123+ const memoryCache = new MemoryCache ( ) ;
124+ const cache = new NonSerializingCache ( memoryCache , ( ) => {
125+ return new Promise ( ( resolve ) => resolve ( testValue ) ) ;
126+ } ) ;
127+
128+ return cache . get ( testKey ) . then ( ( result ) => {
129+ assert ( result === testValue ) ;
130+ } ) . then ( ( ) => {
131+ return memoryCache . get ( testKey ) . then ( ( result ) => {
132+ assert ( result === testValue ) ;
133+ } )
134+ } ) ;
135+ } ) ;
136+
137+ it ( "should only issue one read per request run" , function ( ) {
138+ class TestCache extends MemoryCache {
139+ constructor ( ) {
140+ super ( ) ;
141+ this . counter = 0 ;
142+ }
143+ get ( key ) {
144+ this . counter ++ ;
145+ return super . get ( key ) ;
146+ }
147+ }
148+
149+ const testValue = 'value' ;
150+ const testKey = 'key' ;
151+
152+ const rootCache = new DelayedCache ( new TestCache ( ) , { get : 10 } ) ;
153+ const cache = new NonSerializingCache ( rootCache , ( ) => {
154+ return new Promise ( ( resolve ) => resolve ( testValue ) ) ;
155+ } ) ;
156+
157+ return Promise . all ( [
158+ cache . get ( testKey ) , cache . get ( testKey ) , cache . get ( testKey )
159+ ] ) . then ( ( ) => {
160+ assert . equal ( rootCache . cache . counter , 3 , "Cache should be read three times" ) ;
161+ } ) ;
162+ } ) ;
163+ } ) ;
164+
165+ describe ( 'set' , function ( ) {
166+ it ( 'should not be supported' , function ( ) {
167+ const memoryCache = new MemoryCache ( ) ;
168+ const cache = new NonSerializingCache ( memoryCache , ( ) => {
169+ return new Promise ( ( resolve ) => resolve ( "value" ) ) ;
170+ } ) ;
171+ const testKey = 'key' ;
172+
173+ return cache . set ( testKey , "dummy" ) . then ( ( ) => {
174+ throw Error ( "Set successful" ) ;
175+ } , ( err ) => { } ) ;
176+ } ) ;
177+ } ) ;
178+
179+ describe ( 'del' , function ( ) {
180+ it ( "should fill using callback" , function ( ) {
181+ const testValue = 'value' ;
182+ const testKey = 'key' ;
183+
184+ const memoryCache = new MemoryCache ( ) ;
185+ const cache = new NonSerializingCache ( memoryCache , ( ) => {
186+ return new Promise ( ( resolve ) => resolve ( testValue ) ) ;
187+ } ) ;
188+
189+ return cache . get ( testKey ) . then ( ( result ) => {
190+ assert ( result === testValue ) ;
191+ } ) . then ( ( ) => {
192+ return memoryCache . get ( testKey ) . then ( ( result ) => {
193+ assert ( result === testValue ) ;
194+ } )
195+ } ) ;
196+ } ) ;
197+
198+ it ( "should only issue one delete per request run" , function ( ) {
199+ class TestCache extends MemoryCache {
200+ constructor ( ) {
201+ super ( ) ;
202+ this . counter = 0 ;
203+ }
204+ del ( key ) {
205+ this . counter ++ ;
206+ return super . del ( key ) ;
207+ }
208+ }
209+
210+ const testValue = 'value' ;
211+ const testKey = 'key' ;
212+
213+ const rootCache = new DelayedCache ( new TestCache ( ) , { get : 10 } ) ;
214+ const cache = new NonSerializingCache ( rootCache , ( ) => {
215+ return new Promise ( ( resolve ) => resolve ( testValue ) ) ;
216+ } ) ;
217+
218+ return Promise . all ( [
219+ cache . del ( testKey ) , cache . del ( testKey ) , cache . del ( testKey )
220+ ] ) . then ( ( ) => {
221+ assert . equal ( rootCache . cache . counter , 3 , "Cache entry should be deleted three times" ) ;
222+ } ) ;
223+ } ) ;
224+ } ) ;
225+ } ) ;
0 commit comments