tls_cache_ut.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include <library/cpp/codecs/tls_cache.h>
  3. Y_UNIT_TEST_SUITE(CodecsBufferFactoryTest){
  4. void AssignToBuffer(TBuffer & buf, TStringBuf val){
  5. buf.Assign(val.data(), val.size());
  6. }
  7. TStringBuf AsStringBuf(const TBuffer& b) {
  8. return TStringBuf(b.Data(), b.Size());
  9. }
  10. Y_UNIT_TEST(TestAcquireReleaseReuse) {
  11. NCodecs::TBufferTlsCache factory;
  12. // acquiring the first buffer
  13. auto buf1 = factory.Item();
  14. AssignToBuffer(buf1.Get(), "Buffer_01");
  15. {
  16. // acquiring the second buffer
  17. auto buf2 = factory.Item();
  18. AssignToBuffer(buf2.Get(), "Buffer_02");
  19. }
  20. // the first buffer should stay intact
  21. UNIT_ASSERT_EQUAL(AsStringBuf(buf1.Get()), "Buffer_01");
  22. {
  23. // reacquiring the last released buffer
  24. // expecting it zero sized but having the same memory
  25. auto buf2 = factory.Item();
  26. UNIT_ASSERT_VALUES_EQUAL(buf2.Get().Size(), 0u);
  27. buf2.Get().Resize(TStringBuf("Buffer_02").Size());
  28. UNIT_ASSERT_EQUAL(AsStringBuf(buf2.Get()), "Buffer_02");
  29. }
  30. // when the factory dies we should see no leaks
  31. }
  32. }