Fix cross-thread access to info files. May help with #1618.
[dcpomatic.git] / test / reel_writer_test.cc
1 /*
2     Copyright (C) 2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  test/reel_writer_test.cc
22  *  @brief Test ReelWriter class.
23  *  @ingroup selfcontained
24  */
25
26 #include "lib/reel_writer.h"
27 #include "lib/film.h"
28 #include "lib/cross.h"
29 #include "test.h"
30 #include <boost/test/unit_test.hpp>
31
32 using std::string;
33 using boost::shared_ptr;
34 using boost::optional;
35
36 static bool equal (dcp::FrameInfo a, ReelWriter const & writer, shared_ptr<InfoFileHandle> file, Frame frame, Eyes eyes)
37 {
38         dcp::FrameInfo b = writer.read_frame_info(file, frame, eyes);
39         return a.offset == b.offset && a.size == b.size && a.hash == b.hash;
40 }
41
42 BOOST_AUTO_TEST_CASE (write_frame_info_test)
43 {
44         shared_ptr<Film> film = new_test_film2 ("write_frame_info_test");
45         dcpomatic::DCPTimePeriod const period (dcpomatic::DCPTime(0), dcpomatic::DCPTime(96000));
46         ReelWriter writer (film, period, shared_ptr<Job>(), 0, 1, optional<string>());
47
48         /* Write the first one */
49
50         dcp::FrameInfo info1(0, 123, "12345678901234567890123456789012");
51         writer.write_frame_info (0, EYES_LEFT, info1);
52         shared_ptr<InfoFileHandle> file = film->info_file_handle(period, true);
53
54         BOOST_CHECK (equal(info1, writer, file, 0, EYES_LEFT));
55
56         /* Write some more */
57
58         dcp::FrameInfo info2(596, 14921, "123acb789f1234ae782012n456339522");
59         writer.write_frame_info (5, EYES_RIGHT, info2);
60
61         BOOST_CHECK (equal(info1, writer, file, 0, EYES_LEFT));
62         BOOST_CHECK (equal(info2, writer, file, 5, EYES_RIGHT));
63
64         dcp::FrameInfo info3(12494, 99157123, "xxxxyyyyabc12356ffsfdsf456339522");
65         writer.write_frame_info (10, EYES_LEFT, info3);
66
67         BOOST_CHECK (equal(info1, writer, file, 0, EYES_LEFT));
68         BOOST_CHECK (equal(info2, writer, file, 5, EYES_RIGHT));
69         BOOST_CHECK (equal(info3, writer, file, 10, EYES_LEFT));
70
71         /* Overwrite one */
72
73         dcp::FrameInfo info4(55512494, 123599157123, "ABCDEFGyabc12356ffsfdsf4563395ZZ");
74         writer.write_frame_info (5, EYES_RIGHT, info4);
75
76         BOOST_CHECK (equal(info1, writer, file, 0, EYES_LEFT));
77         BOOST_CHECK (equal(info4, writer, file, 5, EYES_RIGHT));
78         BOOST_CHECK (equal(info3, writer, file, 10, EYES_LEFT));
79 }