func (suite *DynamicChunkStoreSuite) TestSwapReplacesWrapped(c *check.C) {
	provider1 := suite.createChunkProvider(func(consumer chunk.Consumer) {
		consumer.Consume(res.ResourceID(1), chunk.NewBlockHolder(chunk.BasicChunkType, res.Palette, [][]byte{[]byte{}}))
	})
	provider2 := suite.createChunkProvider(func(consumer chunk.Consumer) {
		consumer.Consume(res.ResourceID(2), chunk.NewBlockHolder(chunk.BasicChunkType, res.Palette, [][]byte{[]byte{}}))
	})

	testStore := NewDynamicChunkStore(store.NewProviderBacked(provider1, func() {}))
	testStore.Swap(func(oldStore chunk.Store) chunk.Store {
		return store.NewProviderBacked(provider2, func() {})
	})

	c.Check(testStore.IDs(), check.DeepEquals, []res.ResourceID{res.ResourceID(2)})
}
func (suite *DynamicChunkStoreSuite) TestGetReturnsNilIfWrappedDoesntHaveIt(c *check.C) {
	provider := suite.createChunkProvider(func(consumer chunk.Consumer) {})

	wrappedStore := store.NewProviderBacked(provider, func() {})
	store := NewDynamicChunkStore(wrappedStore)

	holder := store.Get(res.ResourceID(2))

	c.Check(holder, check.IsNil)
}
func (suite *DynamicChunkStoreSuite) TestBlockHolderModifiesWrappedAfterSwap(c *check.C) {
	provider1 := suite.createChunkProvider(func(consumer chunk.Consumer) {
		consumer.Consume(res.ResourceID(1), chunk.NewBlockHolder(chunk.BasicChunkType, res.Palette, [][]byte{[]byte{}}))
	})
	provider2 := suite.createChunkProvider(func(consumer chunk.Consumer) {
		consumer.Consume(res.ResourceID(1), chunk.NewBlockHolder(chunk.BasicChunkType, res.Palette, [][]byte{[]byte{}}))
	})

	testStore := NewDynamicChunkStore(store.NewProviderBacked(provider1, func() {}))
	holder := testStore.Get(res.ResourceID(1))

	wrapped2 := store.NewProviderBacked(provider2, func() {})
	testStore.Swap(func(oldStore chunk.Store) chunk.Store { return wrapped2 })

	data := []byte{0x01, 0x02}
	holder.SetBlockData(0, data)

	c.Check(wrapped2.Get(res.ResourceID(1)).BlockData(0), check.DeepEquals, data)
}
func (suite *DynamicChunkStoreSuite) TestPutInsertsToWrapped(c *check.C) {
	provider := suite.createChunkProvider(func(consumer chunk.Consumer) {})

	wrappedStore := store.NewProviderBacked(provider, func() {})
	store := NewDynamicChunkStore(wrappedStore)

	store.Put(res.ResourceID(1), chunk.NewBlockHolder(chunk.BasicChunkType, res.Palette, [][]byte{[]byte{}}))

	ids := store.IDs()
	c.Check(len(ids), check.Equals, 1)
}
func (suite *DynamicChunkStoreSuite) TestGetReturnsBlockFromWrapped(c *check.C) {
	provider := suite.createChunkProvider(func(consumer chunk.Consumer) {
		consumer.Consume(res.ResourceID(1), chunk.NewBlockHolder(chunk.BasicChunkType, res.Palette, [][]byte{[]byte{}}))
	})

	wrappedStore := store.NewProviderBacked(provider, func() {})
	store := NewDynamicChunkStore(wrappedStore)

	holder := store.Get(res.ResourceID(1))

	c.Check(holder, check.NotNil)
}
func (suite *DynamicChunkStoreSuite) TestBlockHolderModifiesWrapped(c *check.C) {
	provider := suite.createChunkProvider(func(consumer chunk.Consumer) {
		consumer.Consume(res.ResourceID(1), chunk.NewBlockHolder(chunk.BasicChunkType, res.Palette, [][]byte{[]byte{}}))
	})

	wrappedStore := store.NewProviderBacked(provider, func() {})
	store := NewDynamicChunkStore(wrappedStore)

	data := []byte{0x01, 0x02}
	holder := store.Get(res.ResourceID(1))
	holder.SetBlockData(0, data)

	c.Check(wrappedStore.Get(res.ResourceID(1)).BlockData(0), check.DeepEquals, data)
}
func (library *ReleaseStoreLibrary) createSavingChunkStore(provider chunk.Provider, path string, name string, closer func()) chunk.Store {
	storeChanged := make(chan interface{})
	onStoreChanged := func() { storeChanged <- nil }
	chunkStore := NewDynamicChunkStore(storeChunk.NewProviderBacked(provider, onStoreChanged))

	closeLastReader := closer
	saveAndSwap := func() {
		chunkStore.Swap(func(oldStore chunk.Store) chunk.Store {
			log.Printf("Saving resource <%s>/<%s>\n", path, name)
			data := library.serializeChunkStore(oldStore)
			log.Printf("Serialized previous data, closing old reader")
			closeLastReader()

			log.Printf("Recreating new reader for new data")
			newProvider, newReader := library.saveAndReloadChunkData(data, path, name)
			closeLastReader = func() { newReader.Close() }

			return storeChunk.NewProviderBacked(newProvider, onStoreChanged)
		})
	}
	library.startSaverRoutine(storeChanged, saveAndSwap)

	return chunkStore
}