'Test with several replication targets'

def test_many_targets(self):

    doc_managers = [DocManager(), DocManager(), DocManager()]
    self.opman.doc_managers = doc_managers
    self.opman.start()
    self.main_conn['test']['mc'].insert_one({'i': 0})
    self.assertEqual(self.primary_conn['test']['mc'].count(), 1)
    secondary = self.secondary_conn
    assert_soon((lambda : (secondary['test']['mc'].count() == 1)), "first    write    didn't    replicate    to    secondary")
    self.repl_set.primary.stop(destroy=False)
    assert_soon((lambda : secondary.admin.command('isMaster')['ismaster']), 'secondary    was    never    promoted')
    secondary_ids = []
    for i in range(1, 10):
        secondary_ids.append(retry_until_ok(self.main_conn['test']['mc'].insert_one, {'i': i}).inserted_id)
    self.assertEqual(self.secondary_conn['test']['mc'].count(), 10)
    def docmans_done():
        for dm in self.opman.doc_managers:
            if (len(dm._search()) != 10):
                return False
        return True
    assert_soon(docmans_done, 'not    all    writes    were    replicated    to    doc    managers')
    ts = self.opman.doc_managers[0].get_last_doc()['_ts']
    for id in secondary_ids[8:]:
        self.opman.doc_managers[1].remove(id, 'test.mc', ts)
    for id in secondary_ids[2:]:
        self.opman.doc_managers[2].remove(id, 'test.mc', ts)
    self.repl_set.secondary.stop(destroy=False)
    self.repl_set.primary.start()
    primary_admin = self.primary_conn['admin']
    assert_soon((lambda : primary_admin.command('isMaster')['ismaster']), 'restarted    primary    never    resumed    primary    status')
    self.repl_set.secondary.start()
    assert_soon((lambda : (retry_until_ok(secondary.admin.command, 'replSetGetStatus')['myState'] == 2)), 'restarted    secondary    never    resumed    secondary    status')
    assert_soon((lambda : (retry_until_ok(self.primary_conn.test.mc.find().count) > 0)), 'documents    not    found    after    primary/secondary    restarted')
    self.assertEqual(self.primary_conn['test']['mc'].count(), 1)
    self.assertEqual(self.primary_conn['test']['mc'].find_one()['i'], 0)
    time.sleep(10)
    for dm in self.opman.doc_managers:
        self.assertEqual(len(dm._search()), 1)
        self.assertEqual(dm._search()[0]['i'], 0)
    self.opman.join()
