Skip to content

Instantly share code, notes, and snippets.

@IliaSky
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IliaSky/5d5230ec306a8a5bc4fe to your computer and use it in GitHub Desktop.
Save IliaSky/5d5230ec306a8a5bc4fe to your computer and use it in GitHub Desktop.
Test for a golang homework
package main
import (
"runtime"
"testing"
"time"
)
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}
const DELTA = 10 * time.Millisecond
func TestSettingAndGettingAKey(t *testing.T) {
em := NewExpireMap()
defer em.Destroy()
key := "foo"
expected := "bar"
em.Set(key, expected, 15*time.Second)
found, ok := em.Get(key)
if ok == false {
t.Errorf("Getting the foo key failed")
}
if found != expected {
t.Error("Found key was different than the expected")
}
_, ok = em.Get("not-there")
if ok {
t.Error("Found key which shouldn't have been there")
}
}
func TestGettingExpiredValues(t *testing.T) {
em := NewExpireMap()
defer em.Destroy()
duration := 150 * time.Millisecond
em.Set("foo", "super-bar", duration)
em.Set("bar", "super-foo", 300*time.Millisecond)
time.Sleep(duration + DELTA)
if _, ok := em.GetString("foo"); ok {
t.Errorf("Getting expired key did not return error")
}
expected := "super-foo"
found, ok := em.GetString("bar")
if ok == false {
t.Errorf("Getting non-expired key returned error")
}
if expected != found {
t.Errorf("Expected %s but found %s", expected, found)
}
}
func TestCleaningUpTheMap(t *testing.T) {
em := NewExpireMap()
defer em.Destroy()
em.Set("foo", "bar", 15*time.Second)
em.Set("number", "794", 15*time.Second)
em.Cleanup()
if em.Size() != 0 {
t.Errorf("Cleaning up ExpireMap did not work")
}
}
func TestTheExampleInReadme(t *testing.T) {
em := NewExpireMap()
defer em.Destroy()
em.Set("foo", "bar", 15*time.Second)
em.Set("spam", "4", 25*time.Minute)
em.Set("eggs", 9000.01, 3*time.Minute)
err := em.Increment("spam")
if err != nil {
t.Error("Sadly, incrementing the spam did not succeed")
}
if val, ok := em.Get("spam"); ok {
if val != "5" {
t.Error("spam was not 5")
}
} else {
t.Error("No spam. Have some eggs instead?")
}
if eggs, ok := em.GetFloat64("eggs"); !ok || eggs <= 9000 {
t.Error("We did not have as many eggs as expected.",
"Have you considered our spam offers?")
}
}
func TestExpiredChanExample(t *testing.T) {
em := NewExpireMap()
defer em.Destroy()
em.Set("key1", "val1", 50*time.Millisecond)
em.Set("key2", "val2", 100*time.Millisecond)
expires := em.ExpiredChan()
for i := 0; i < 2; i++ {
select {
case <-expires:
// nothing to do
case <-time.After(50*time.Millisecond + DELTA*2):
t.Fatal("Expired key was not read from the channel on time")
}
}
}
func TestIncrementNonExistentValue(t *testing.T) {
em := NewExpireMap()
defer em.Destroy()
err := em.Increment("non-existent")
if err == nil {
t.Error("Incrementing a non-existent value did not return an error")
}
}
func TestSetMultipleExpirationTimes(t *testing.T) {
em := NewExpireMap()
defer em.Destroy()
em.Set("a", "a", 100*time.Millisecond)
em.Set("a", "b", 400*time.Millisecond)
time.Sleep(200 * time.Millisecond)
if !em.Contains("a") {
t.Error("Key expired even though the expiration time should've been extended")
}
}
func TestOverridenKeyExpiredChannelResult(t *testing.T) {
em := NewExpireMap()
defer em.Destroy()
expires := em.ExpiredChan()
em.Set("a", "a", 100*time.Millisecond)
em.Set("a", "b", 200*time.Millisecond)
receivedCount := 0
for i := 0; i < 2; i++ {
select {
case <-expires:
receivedCount++
case <-time.After(300 * time.Millisecond):
}
}
if receivedCount > 1 {
t.Error("Expired chan received overriden key more than once")
}
}
func TestWorkForAWhile(t *testing.T) {
em := NewExpireMap()
em.Set("a", 5, time.Millisecond)
em.Destroy()
time.Sleep(500 * time.Millisecond)
}
func TestExpiredChanDoesNotReceiveDeletedKeys(t *testing.T) {
em := NewExpireMap()
expires := em.ExpiredChan()
em.Set("a", 5, 50*time.Millisecond)
em.Delete("a")
select {
case <-expires:
t.Error("Expired chan received a key when it was deleted")
case <-time.After(20 * time.Millisecond):
}
select {
case <-expires:
t.Error("Expired chan received a key after it was deleted")
case <-time.After(100 * time.Millisecond):
}
}
func TestExpiredChanDoesNotReceiveCleanedKeys(t *testing.T) {
em := NewExpireMap()
expires := em.ExpiredChan()
em.Set("a", 5, 200*time.Millisecond)
em.Set("b", 4, 200*time.Millisecond)
em.Cleanup()
for i := 0; i < 2; i++ {
select {
case _, ok := <-expires:
if ok {
t.Error("Expired chan should not receive destroyed keys")
}
case <-time.After(300 * time.Millisecond):
}
}
}
func TestExpiredChanDoesNotReceiveDestroyedKeys(t *testing.T) {
em := NewExpireMap()
expires := em.ExpiredChan()
em.Set("a", 5, 200*time.Millisecond)
em.Set("b", 4, 200*time.Millisecond)
em.Destroy()
for i := 0; i < 2; i++ {
select {
case _, ok := <-expires:
if ok {
t.Error("Expired chan should not receive destroyed keys")
}
case <-time.After(300 * time.Millisecond):
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment