This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delete key feature added with its tests. Example usage added to pkg/main.go and README. README updated. Signed-off-by: Gökhan Özeloğlu <gozeloglu@gmail.com>
- Loading branch information
Showing
4 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package kvs | ||
|
||
func (k *Kvs) Del(key string) { | ||
delete(k.kv, key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package kvs | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestDel(t *testing.T) { | ||
db, err := open(t.Name(), "", 2*time.Minute) | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
t.Logf("db created.") | ||
|
||
k, v := "foo", "bar" | ||
db.Set(k, v) | ||
|
||
val := db.Get(k) | ||
if val != v { | ||
t.Logf("set value could not get.") | ||
} | ||
|
||
db.Del(k) | ||
if db.Get(k) != "" { | ||
t.Logf("key could not deleted.") | ||
} | ||
|
||
t.Logf("Key is deleted successfully.") | ||
|
||
err = db.Close() | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
t.Logf("db closed.") | ||
|
||
err = os.RemoveAll(db.dir) | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
t.Logf("test file removed.") | ||
} | ||
|
||
func TestDelWithTime(t *testing.T) { | ||
db, err := open(t.Name(), "", 2*time.Second) | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
t.Logf("db created.") | ||
|
||
k, v := "foo", "bar" | ||
db.Set(k, v) | ||
val := db.Get(k) | ||
t.Logf("%s: %s", k, val) | ||
db.Del(k) | ||
val = db.Get(k) | ||
if val != "" { | ||
t.Errorf("val is not empty string: %s", val) | ||
} | ||
time.Sleep(3 * time.Second) | ||
|
||
err = db.Close() | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
t.Logf("db closed.") | ||
|
||
tmpDb, err := open(t.Name(), "", 2*time.Minute) | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
t.Logf("tmpdb created.") | ||
|
||
val = tmpDb.Get(k) | ||
if val != "" { | ||
t.Errorf("val is not empty string: %s", val) | ||
} | ||
|
||
err = tmpDb.Close() | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
t.Logf("db closed.") | ||
|
||
err = os.RemoveAll(tmpDb.dir) | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
t.Logf("tmp file removed.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters