@@ -145,3 +145,58 @@ func TestRepoAccessCacheSetTTLReschedulesExistingEntry(t *testing.T) {
145145 requireAccess (ctx , t , cache )
146146 require .EqualValues (t , 2 , transport .CallCount ())
147147}
148+
149+ func TestGetInstanceReturnsSingleton (t * testing.T ) {
150+ // Reset any existing singleton
151+ ResetInstance ()
152+ defer ResetInstance () // Clean up after test
153+
154+ gqlClient := githubv4 .NewClient (nil )
155+
156+ // Get instance twice, should return the same instance
157+ instance1 := GetInstance (gqlClient )
158+ instance2 := GetInstance (gqlClient )
159+
160+ // Verify they're the same instance (same pointer)
161+ require .Same (t , instance1 , instance2 , "GetInstance should return the same singleton instance" )
162+
163+ // Verify subsequent calls with different options are ignored
164+ instance3 := GetInstance (gqlClient , WithTTL (1 * time .Second ))
165+ require .Same (t , instance1 , instance3 , "GetInstance should ignore options on subsequent calls" )
166+ require .Equal (t , defaultRepoAccessTTL , instance3 .ttl , "TTL should remain unchanged after first initialization" )
167+ }
168+
169+ func TestResetInstanceClearsSingleton (t * testing.T ) {
170+ // Reset any existing singleton
171+ ResetInstance ()
172+ defer ResetInstance () // Clean up after test
173+
174+ gqlClient := githubv4 .NewClient (nil )
175+
176+ // Get first instance with default TTL
177+ instance1 := GetInstance (gqlClient )
178+ require .Equal (t , defaultRepoAccessTTL , instance1 .ttl )
179+
180+ // Reset the singleton
181+ ResetInstance ()
182+
183+ // Get new instance with custom TTL
184+ customTTL := 10 * time .Second
185+ instance2 := GetInstance (gqlClient , WithTTL (customTTL ))
186+ require .NotSame (t , instance1 , instance2 , "After reset, GetInstance should return a new instance" )
187+ require .Equal (t , customTTL , instance2 .ttl , "New instance should have the custom TTL" )
188+ }
189+
190+ func TestNewRepoAccessCacheCreatesIndependentInstances (t * testing.T ) {
191+ t .Parallel ()
192+
193+ gqlClient := githubv4 .NewClient (nil )
194+
195+ // NewRepoAccessCache should create independent instances
196+ cache1 := NewRepoAccessCache (gqlClient , WithTTL (1 * time .Second ))
197+ cache2 := NewRepoAccessCache (gqlClient , WithTTL (2 * time .Second ))
198+
199+ require .NotSame (t , cache1 , cache2 , "NewRepoAccessCache should create different instances" )
200+ require .Equal (t , 1 * time .Second , cache1 .ttl )
201+ require .Equal (t , 2 * time .Second , cache2 .ttl )
202+ }
0 commit comments