sapling/eden/mononoke/blobrepo/test/tracing_blobstore.rs
Lukas Piatkowski fa1a195fd0 mononoke/blobstore: pass CoreContext via borrowed instead of owned value
Summary: Follow up after removing 'static from blobstore.

Reviewed By: StanislavGlebik

Differential Revision: D25182106

fbshipit-source-id: e13a7a31d71b4674425123268e655ae66127f1b7
2020-11-27 03:31:07 -08:00

62 lines
1.5 KiB
Rust

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
use anyhow::Result;
use async_trait::async_trait;
use blobstore::{Blobstore, BlobstoreGetData};
use context::CoreContext;
use mononoke_types::BlobstoreBytes;
use std::sync::{Arc, Mutex};
#[derive(Debug)]
pub struct TracingBlobstore<T> {
inner: T,
gets: Arc<Mutex<Vec<String>>>,
}
impl<T> TracingBlobstore<T> {
pub fn new(inner: T) -> Self {
let gets = Arc::new(Mutex::new(vec![]));
Self { inner, gets }
}
}
impl<T> TracingBlobstore<T> {
pub fn tracing_gets(&self) -> Vec<String> {
let mut gets = self.gets.lock().expect("poisoned lock");
std::mem::replace(&mut *gets, vec![])
}
}
#[async_trait]
impl<T: Blobstore> Blobstore for TracingBlobstore<T> {
async fn get<'a>(
&'a self,
ctx: &'a CoreContext,
key: &'a str,
) -> Result<Option<BlobstoreGetData>> {
self.gets
.lock()
.expect("poisoned lock")
.push(key.to_owned());
self.inner.get(ctx, key).await
}
async fn put<'a>(
&'a self,
ctx: &'a CoreContext,
key: String,
value: BlobstoreBytes,
) -> Result<()> {
self.inner.put(ctx, key, value).await
}
async fn is_present<'a>(&'a self, ctx: &'a CoreContext, key: &'a str) -> Result<bool> {
self.inner.is_present(ctx, key).await
}
}