package cache

import (
	"context"
	"log"
	"sync"
	"testing"
	"time"
)

func TestChan(t *testing.T) {
	t.Log("!!!")
	ch := make(chan int, 1)

	for i := 0; i < 5; i++ {
		go func(im int) {
			for val := range ch {
				t.Log(im, val)
			}
		}(i)
	}

	for i := 0; i < 10; i++ {
		ch <- i
		time.Sleep(time.Second * 2)
	}
}

func TestContext(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		log.Println("OK")
		<-ctx.Done()
		log.Println("Context done")
		//wg.Add(1)
		go func() {
			log.Println("ST")
			<-ctx.Done()
			log.Println("Context done 2")
		}()
	}()
	time.Sleep(time.Second * 2)
	cancel()
	wg.Wait()
}

func TestCacheMap(t *testing.T) {
	cm := NewCacheMap(context.Background(), 10, LiveInfinite)
	for i := 0; i < 10; i++ {
		cm.Set(i, i)
		t.Log(cm)
		time.Sleep(time.Second)
	}
	ko := cm.keysOlder(3)
	t.Log(ko)
}