~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_groupcompress.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-04-09 20:23:07 UTC
  • mfrom: (4265.1.4 bbc-merge)
  • Revision ID: pqm@pqm.ubuntu.com-20090409202307-n0depb16qepoe21o
(jam) Change _fetch_uses_deltas = False for CHK repos until we can
        write a better fix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import zlib
20
20
 
21
21
from bzrlib import (
22
 
    btree_index,
23
22
    groupcompress,
24
23
    errors,
25
 
    index as _mod_index,
26
24
    osutils,
27
25
    tests,
28
 
    trace,
29
26
    versionedfile,
30
27
    )
31
28
from bzrlib.osutils import sha_string
122
119
                          'different\n', sha1_2),
123
120
                         compressor.extract(('newlabel',)))
124
121
 
125
 
    def test_pop_last(self):
126
 
        compressor = self.compressor()
127
 
        _, _, _, _ = compressor.compress(('key1',),
128
 
            'some text\nfor the first entry\n', None)
129
 
        expected_lines = list(compressor.chunks)
130
 
        _, _, _, _ = compressor.compress(('key2',),
131
 
            'some text\nfor the second entry\n', None)
132
 
        compressor.pop_last()
133
 
        self.assertEqual(expected_lines, compressor.chunks)
134
 
 
135
122
 
136
123
class TestPyrexGroupCompressor(TestGroupCompressor):
137
124
 
364
351
        raw_bytes = zlib.decompress(remaining_bytes)
365
352
        self.assertEqual(content, raw_bytes)
366
353
 
367
 
        # we should get the same results if using the chunked version
368
 
        gcb = groupcompress.GroupCompressBlock()
369
 
        gcb.set_chunked_content(['this is some content\n'
370
 
                                 'this content will be compressed\n'],
371
 
                                 len(content))
372
 
        old_bytes = bytes
373
 
        bytes = gcb.to_bytes()
374
 
        self.assertEqual(old_bytes, bytes)
375
 
 
376
354
    def test_partial_decomp(self):
377
355
        content_chunks = []
378
356
        # We need a sufficient amount of data so that zlib.decompress has
459
437
        # And the decompressor is finalized
460
438
        self.assertIs(None, block._z_content_decompressor)
461
439
 
462
 
    def test__dump(self):
463
 
        dup_content = 'some duplicate content\nwhich is sufficiently long\n'
464
 
        key_to_text = {('1',): dup_content + '1 unique\n',
465
 
                       ('2',): dup_content + '2 extra special\n'}
466
 
        locs, block = self.make_block(key_to_text)
467
 
        self.assertEqual([('f', len(key_to_text[('1',)])),
468
 
                          ('d', 21, len(key_to_text[('2',)]),
469
 
                           [('c', 2, len(dup_content)),
470
 
                            ('i', len('2 extra special\n'), '')
471
 
                           ]),
472
 
                         ], block._dump())
473
 
 
474
440
 
475
441
class TestCaseWithGroupCompressVersionedFiles(tests.TestCaseWithTransport):
476
442
 
477
443
    def make_test_vf(self, create_graph, keylength=1, do_cleanup=True,
478
 
                     dir='.', inconsistency_fatal=True):
 
444
                     dir='.'):
479
445
        t = self.get_transport(dir)
480
446
        t.ensure_base()
481
447
        vf = groupcompress.make_pack_factory(graph=create_graph,
482
 
            delta=False, keylength=keylength,
483
 
            inconsistency_fatal=inconsistency_fatal)(t)
 
448
            delta=False, keylength=keylength)(t)
484
449
        if do_cleanup:
485
450
            self.addCleanup(groupcompress.cleanup_pack_group, vf)
486
451
        return vf
488
453
 
489
454
class TestGroupCompressVersionedFiles(TestCaseWithGroupCompressVersionedFiles):
490
455
 
491
 
    def make_g_index(self, name, ref_lists=0, nodes=[]):
492
 
        builder = btree_index.BTreeBuilder(ref_lists)
493
 
        for node, references, value in nodes:
494
 
            builder.add_node(node, references, value)
495
 
        stream = builder.finish()
496
 
        trans = self.get_transport()
497
 
        size = trans.put_file(name, stream)
498
 
        return btree_index.BTreeGraphIndex(trans, name, size)
499
 
 
500
 
    def make_g_index_missing_parent(self):
501
 
        graph_index = self.make_g_index('missing_parent', 1,
502
 
            [(('parent', ), '2 78 2 10', ([],)),
503
 
             (('tip', ), '2 78 2 10',
504
 
              ([('parent', ), ('missing-parent', )],)),
505
 
              ])
506
 
        return graph_index
507
 
 
508
456
    def test_get_record_stream_as_requested(self):
509
457
        # Consider promoting 'as-requested' to general availability, and
510
458
        # make this a VF interface test
636
584
            else:
637
585
                self.assertIs(block, record._manager._block)
638
586
 
639
 
    def test_add_missing_noncompression_parent_unvalidated_index(self):
640
 
        unvalidated = self.make_g_index_missing_parent()
641
 
        combined = _mod_index.CombinedGraphIndex([unvalidated])
642
 
        index = groupcompress._GCGraphIndex(combined,
643
 
            is_locked=lambda: True, parents=True,
644
 
            track_external_parent_refs=True)
645
 
        index.scan_unvalidated_index(unvalidated)
646
 
        self.assertEqual(
647
 
            frozenset([('missing-parent',)]), index.get_missing_parents())
648
 
 
649
 
    def test_track_external_parent_refs(self):
650
 
        g_index = self.make_g_index('empty', 1, [])
651
 
        mod_index = btree_index.BTreeBuilder(1, 1)
652
 
        combined = _mod_index.CombinedGraphIndex([g_index, mod_index])
653
 
        index = groupcompress._GCGraphIndex(combined,
654
 
            is_locked=lambda: True, parents=True,
655
 
            add_callback=mod_index.add_nodes,
656
 
            track_external_parent_refs=True)
657
 
        index.add_records([
658
 
            (('new-key',), '2 10 2 10', [(('parent-1',), ('parent-2',))])])
659
 
        self.assertEqual(
660
 
            frozenset([('parent-1',), ('parent-2',)]),
661
 
            index.get_missing_parents())
662
 
 
663
 
    def make_source_with_b(self, a_parent, path):
664
 
        source = self.make_test_vf(True, dir=path)
665
 
        source.add_lines(('a',), (), ['lines\n'])
666
 
        if a_parent:
667
 
            b_parents = (('a',),)
668
 
        else:
669
 
            b_parents = ()
670
 
        source.add_lines(('b',), b_parents, ['lines\n'])
671
 
        return source
672
 
 
673
 
    def do_inconsistent_inserts(self, inconsistency_fatal):
674
 
        target = self.make_test_vf(True, dir='target',
675
 
                                   inconsistency_fatal=inconsistency_fatal)
676
 
        for x in range(2):
677
 
            source = self.make_source_with_b(x==1, 'source%s' % x)
678
 
            target.insert_record_stream(source.get_record_stream(
679
 
                [('b',)], 'unordered', False))
680
 
 
681
 
    def test_inconsistent_redundant_inserts_warn(self):
682
 
        """Should not insert a record that is already present."""
683
 
        warnings = []
684
 
        def warning(template, args):
685
 
            warnings.append(template % args)
686
 
        _trace_warning = trace.warning
687
 
        trace.warning = warning
688
 
        try:
689
 
            self.do_inconsistent_inserts(inconsistency_fatal=False)
690
 
        finally:
691
 
            trace.warning = _trace_warning
692
 
        self.assertEqual(["inconsistent details in skipped record: ('b',)"
693
 
                          " ('42 32 0 8', ((),)) ('74 32 0 8', ((('a',),),))"],
694
 
                         warnings)
695
 
 
696
 
    def test_inconsistent_redundant_inserts_raises(self):
697
 
        e = self.assertRaises(errors.KnitCorrupt, self.do_inconsistent_inserts,
698
 
                              inconsistency_fatal=True)
699
 
        self.assertContainsRe(str(e), "Knit.* corrupt: inconsistent details"
700
 
                              " in add_records:"
701
 
                              " \('b',\) \('42 32 0 8', \(\(\),\)\) \('74 32"
702
 
                              " 0 8', \(\(\('a',\),\),\)\)")
703
 
 
704
587
 
705
588
class TestLazyGroupCompress(tests.TestCaseWithTransport):
706
589