diff --git a/.github/workflows/test-coherence.yml b/.github/workflows/test-coherence.yml index f2d4879f..538d187f 100644 --- a/.github/workflows/test-coherence.yml +++ b/.github/workflows/test-coherence.yml @@ -23,23 +23,7 @@ jobs: - name: Startup Coherence run: | docker run -d -p 1408:1408 -p 30000:30000 ghcr.io/oracle/coherence-ce:22.06.5 - counter=0 - while : - do - curl -v -X GET http://localhost:30000/management/coherence/cluster/health/ready > /dev/null 2>&1 - ret=$? - if [ $ret -eq 0 ] ; then - break - else - let counter=counter+1 - echo "Coherence not ready, counter=$counter" - if [ $counter -ge 10 ] ; then - echo "Not ready after 60 seconds, exiting" - exit 1 - fi - fi - sleep 6 - done + sleep 30 - name: Install Go uses: actions/setup-go@v4 with: diff --git a/coherence/coherence.go b/coherence/coherence.go index 949db518..59396ac3 100644 --- a/coherence/coherence.go +++ b/coherence/coherence.go @@ -10,7 +10,7 @@ import ( ) const ( - defaultScopeName = "fiber-session-store" + defaultScopeName = "default-store" defaultTimeout = time.Duration(30) * time.Millisecond ) @@ -29,19 +29,23 @@ type Config struct { // UseSSL specified if to use SSL or plain text, defaults to false UseSSL bool - // SessionTimeout is the default session timeout to connect to Coherence, defaults to 30s - SessionTimeout time.Duration + // Timeout is the default session timeout to connect to Coherence, defaults to 30s + Timeout time.Duration - // SessionScope defines a scope allowing for multiple storage sessions - SessionScope string + // ScopeName defines a scope allowing for multiple storage sessions + ScopeName string + + // Reset indicates if the store should be reset after being created + Reset bool } // DefaultConfig defines default options. var DefaultConfig = Config{ - Address: "localhost:1408", - UseSSL: false, - SessionTimeout: time.Duration(30) * time.Millisecond, - SessionScope: defaultScopeName, + Address: "localhost:1408", + UseSSL: false, + Timeout: time.Duration(30) * time.Millisecond, + ScopeName: defaultScopeName, + Reset: false, } // New returns a new [Storage] given a [coherence.Session]. @@ -67,12 +71,12 @@ func New(config ...Config) (*Storage, error) { options = append(options, coh.WithPlainText()) } - if cfg.SessionTimeout != defaultTimeout { - options = append(options, coh.WithReadyTimeout(cfg.SessionTimeout)) + if cfg.Timeout != defaultTimeout { + options = append(options, coh.WithReadyTimeout(cfg.Timeout)) } - if cfg.SessionScope != defaultScopeName { - scopeName = cfg.SessionScope + if cfg.ScopeName != defaultScopeName { + scopeName = cfg.ScopeName } // create the Coherence session @@ -81,7 +85,17 @@ func New(config ...Config) (*Storage, error) { return nil, err } - return newCoherenceStorage(session, scopeName) + store, err := newCoherenceStorage(session, scopeName) + if err != nil { + return nil, err + } + + // if REset is true then reset the store + if cfg.Reset { + return store, store.Reset() + } + + return store, nil } // newCoherenceStorage returns a new Coherence [Storage]. @@ -126,3 +140,7 @@ func (s *Storage) Close() error { s.session.Close() return nil } + +func (s *Storage) Conn() *coh.Session { + return s.namedCache.GetSession() +} diff --git a/coherence/coherence_test.go b/coherence/coherence_test.go index 3ec97672..666aba81 100644 --- a/coherence/coherence_test.go +++ b/coherence/coherence_test.go @@ -30,6 +30,8 @@ func Test_Coherence_Set_And_Get(t *testing.T) { utils.AssertEqual(t, err, nil) utils.AssertEqual(t, value1, val) + utils.AssertEqual(t, true, testStore.Conn() != nil) + utils.AssertEqual(t, testStore.Close(), nil) } @@ -52,6 +54,30 @@ func Test_Coherence_Set_Override(t *testing.T) { utils.AssertEqual(t, testStore.Close(), nil) } +func Test_Coherence_Set_With_Reset(t *testing.T) { + var val []byte + + testStore, err := newTestStore(t) + utils.AssertEqual(t, err, nil) + + err = testStore.Set(key1, value1, 0) + utils.AssertEqual(t, err, nil) + + val, err = testStore.Get(key1) + utils.AssertEqual(t, err, nil) + utils.AssertEqual(t, value1, val) + + // get a new store but reset it, so the subsequent Get will return nil + testStore, err = newTestStore(t, Config{Reset: true}) + utils.AssertEqual(t, err, nil) + + val, err = testStore.Get(key1) + utils.AssertEqual(t, err, nil) + utils.AssertEqual(t, true, len(val) == 0) + + utils.AssertEqual(t, testStore.Close(), nil) +} + func Test_Coherence_Set_With_Expiry(t *testing.T) { var val []byte @@ -144,10 +170,10 @@ func Test_Coherence_With_Scope(t *testing.T) { var val []byte // create two session stores with different scopes - testStore1, err := newTestStore(t, Config{SessionScope: "scope1"}) + testStore1, err := newTestStore(t, Config{ScopeName: "scope1"}) utils.AssertEqual(t, err, nil) - testStore2, err := newTestStore(t, Config{SessionScope: "scope2"}) + testStore2, err := newTestStore(t, Config{ScopeName: "scope2"}) utils.AssertEqual(t, err, nil) // ensure we can put the same key with different values in each scope